Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 18th, 2004, 12:25 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Assign value from DataReader to session??


Hey guys, I want to assign a value from my DataReader into a session object but I keep getting errors. Basically I want a database value retrieved and stored into a session object. Here's what I got:

Code:
protected void Page_Load(Object sender, EventArgs e){
}

protected void Login_OnClick(Object sender, EventArgs e){

 string SqlStmt = "SELECT COUNT(*) FROM Users WHERE user_name='" +
                   UserName.Text.Trim() + "' " + "AND password = '" + Password.Text.Trim() + "'";


 OleDbConnection con = new
   OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\net\MyData.mdb;");
 OleDbCommand cmd = new OleDbCommand(SqlStmt, con);
 OleDbDataReader reader = null;




 con.Open();
 reader = cmd.ExecuteReader();


 while(reader.Read()){
  if(Int32.Parse(reader[0].ToString()) >= 1)
  {

  Session["user"] = reader("user_name"); //THIS IS WHERE I'M MESSING UP

   FormsAuthentication.RedirectFromLoginPage(UserName.Text,
     PersistCookie.Checked);
  }else{
   ErrorMsg.Text = "<p><b>" +
     "User not found, or bad credentials - try again!" +
     "</b></p>";
  }
 }
}

protected void Signout_OnClick(Object sender, EventArgs e){
 FormsAuthentication.SignOut();
 HttpContext.Current.Response.Redirect("login_database.aspx");
}
HERE IS THE ERROR I'M GETTING, IT'S ON LINE 32:

Compiler Error Message: CS0118: 'reader' denotes a 'variable' where a 'method' was expected

Source Error:

Line 30: {
Line 31:
Line 32: Session["user"] = reader("user_name").ToString();


Any Ideas?

Thanks guys!

Take care,

Chris
 
Old November 18th, 2004, 01:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try reader["user_name"]

 
Old November 18th, 2004, 02:32 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey. I tried that and I got this error:

System.Index.OutOfRangeExecption: user_name

I also tried: reader["user_name"].ToString();

Any Ideas?

Thanks man,

Chris

 
Old November 18th, 2004, 03:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Just saw this:
"SELECT COUNT(*) FROM Users WHERE user_name='" +
                   UserName.Text.Trim() + "' " + "AND password = '" + Password.Text.Trim() + "'";

Replace with user_name

 
Old November 18th, 2004, 03:29 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey thanks, I feel we are getting there, however I would still need COUNT(*) in order for the login authentication to work. So I tried adding user_name after COUNT(*) (ex. select COUNT(*), user_name ) and recieved this error:

System.Data.OleDb.OleDbException: You tried to execute a query that does not include the specified expression 'user_name' as part of an aggregate function.

I really appreciate your helping me.

-Chris



 
Old November 18th, 2004, 04:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try adding Group By user_name at the end of your SQL statement.

 
Old November 18th, 2004, 04:52 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HOLY CRAP!! IT WORKED!! thank you thank you thank you. You are the only person that solved this problem for me.
Why did it need the group by clause? I'm perplexed.

THANKS!!!!!

 
Old November 18th, 2004, 05:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Count is a group function and if you add another column to the end you need to tell SQL to group on the added column name. Someone else might be able to explain it better.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign Session variable from view or controller Alaphat ASP.NET 2.0 Professional 2 April 11th, 2008 09:37 AM
How To Assign Value To Session Using Javascript janice.koay Classic ASP Professional 1 May 9th, 2006 12:59 AM
DataReader truongnnhat ASP.NET 1.0 and 1.1 Basics 2 February 18th, 2005 12:41 AM
Datareader NitinJoshi ADO.NET 4 January 31st, 2005 08:34 AM
DataReader cjcd BOOK: Beginning ASP.NET 1.0 2 March 21st, 2004 01:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.