Last post Jan 26, 2011 01:36 PM by csolution
Jan 26, 2011 11:49 AM|csolution|LINK
I'm starting to learn .NET (3.5) with c# as the coding language, and i'm a little stuck as to why the query is not able to return data from a table, that i know exists.
Im using Visual Web Developer 2008, and i've used the wizard to generate a quick login feature. The initial script was able to login successfully, validating the user details with those entered in the textboxes..
I've now altered the table to include and additional column "Level". It's associated with the user account. So based on the level of the account range (1-10) different options appear to the user on the main page.
Below is the code that i am using for the login validation, it's an edit; of the functioning login wizard that is supplied by VWB2008.
The connection string is working correctly, which is stored in the web.config file.
The code is as below with the problem highlighted in bold.
The error returns "System.InvalidOperationException: Invalid attempt to read when no data is present.". I've checked and double checked the case sensitivity, but to no avail. Is the error
indicating that the user account cannot be located, or that there is no column/data in the matching account?
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
string username = Login1.UserName;
string pwd = Login1.Password;
strConn = WebConfigurationManager.ConnectionStrings["ConnectionLogin"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
sqlUserName = "SELECT Level FROM tblLogin ";
sqlUserName += " WHERE (Username ='" + username + "')";
sqlUserName += " AND (Password ='" + pwd + "')";
SqlCommand com = new SqlCommand(sqlUserName, Conn);
SqlDataReader myReader = com.ExecuteReader();
Session["UserAuthentication"] = username;
Session["uLevel"] = myReader["Level"];
Session.Timeout = 25;
myReader = null;
Session["UserAuthentication"] = "";
Thanks for any help,
Jan 26, 2011 12:26 PM|rivdiv|LINK
Use: while (myReader.Read())
Jan 26, 2011 01:36 PM|csolution|LINK
that fixed it, many thanks!