I have an assignment in ASP.NET Using Visual Studio
I have created three web forms and connected them to the database(Register webform, login web form , UserDetailWebform)
When I use the login page it will send me to the userDetail page, in userDetail page, It always shows me the first Full name and Email for the first account on database, and for the other account it will show nothing (the label will not change)
Here is my database
Ex: when I press login on the login page I want to see Kelly's Fullname and her Email, in my code below only I can see the Fullname and email for the first account other account I cannot retrieve thier Fullname and email
the code that I use in the UserDetail Page
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
string checkUser = "select * from userDetail";
SqlCommand com = new SqlCommand(checkUser, con);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
if (Session["Email"].ToString() == dr["Email"].ToString())
{
Label1.Text = dr["Fname"].ToString();
Label2.Text = dr["Email"].ToString();
}
}
}
CodePudding user response:
Well, the Read() command only reads one row at a time.
I suggest you do it this way:
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string strSQL = "SELECT * FROM userDetail WHERE email = @email";
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
conn.Open();
cmdSQL.Parameters.Add("@email", SqlDbType.NVarChar).Value = Session["Email"];
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
Label1.Text = rstData.Rows[0]["Fname"].ToString();
Label2.Text = rstData.Rows[0]["Email"].ToString();
}
}