I get an error in my code: Cannot find table 0. what am I doing wrong?
OdbcCommand cmd = new OdbcCommand("Select * from User where username=? and password=?", cn);
DataSet ds = new DataSet();
//Select the username and password from mysql database in login table
cmd.Parameters.Add("@username", OdbcType.VarChar);
cmd.Parameters["@username"].Value = this.Login1.UserName;
cmd.Parameters.Add("@password", OdbcType.VarChar);
cmd.Parameters["@password"].Value = this.Login1.Password;
//use asp login control to check username and password
//Session["UserID"] = "usrName";
//set the UserID from the User Table unsure how to add this to the sql syntax above
OdbcDataReader dr = default(OdbcDataReader);
// Initialise a reader to read the rows from the login table.
// If row exists, the login is successful
dr = cmd.ExecuteReader();
DataTable dt = ds.Tables[0];
DataRow dp = dt.Rows[0];
if (dt.Rows.Count != 0)
{
Session["UserID"] = Convert.ToString(dp["UserID"]);
e.Authenticated = true;
Response.Redirect("UserProfileWall.aspx");
// Event Authenticate is true forward to user profile
}
}
}
Your code creates an empty dataset, then tries to get a table from it.
Since you never put anything in the dataset, you get an error.
You need to use the
DataReaderthat you got back from your query (use theHasRowsproperty).However, I strongly recommend that you
Use ASP.Net’s built-in forms authentication system
instead. It will save you lots of code and is more secure.