locked
return ID asp.net RRS feed

  • Question

  • User639567535 posted

    i want to return userid against username and password 

    e.g. 

    1  abc  a

    11 def  ab

    when i enter abc in username textbox and a in password textbox then want to return 1 in label how i modify this code 

     protected void Button4_Click(object sender, EventArgs e)
            {
                string query = @"Data Source=HOME\SQL;Initial Catalog=The_Coffe;Integrated Security=True";
                SqlConnection con = new SqlConnection(query);
                con.Open();
                string b = "Select C_ID from Customer_Register where Email='" + TextBox1.Text + "'  AND password='" + TextBox2.Text + "'";
                SqlCommand cam = new SqlCommand(b, con);
                cam.Parameters.AddWithValue("@Email", TextBox1.Text);
                cam.Parameters.AddWithValue("@password", TextBox2.Text);
                cam.ExecuteNonQuery();
                SqlDataReader dr = cam.ExecuteReader();
    
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        TextBox1.Text = Convert.ToString(dr["Email"]);
                        TextBox2.Text = Convert.ToString(dr["password"]);
                         Session["UserName"] = TextBox1.Text;
                         Session["UserID"] = ID.Text;
                        //Response.Redirect("Fronpa.aspx");
    
                    }
                   
    
    
    
                }
                else
                {
    
    
                    Label4.Visible = true;
                    Label4.Text = "Incorrect Email or Password..";
    
                }
                con.Close();
            }

    Wednesday, April 11, 2018 7:15 PM

Answers

  • User-1716253493 posted
                    while (dr.Read())
                    {
                        //TextBox1.Text = Convert.ToString(dr["Email"]);
                        //TextBox2.Text = Convert.ToString(dr["password"]);
                        Label1.Text = Convert.ToString(dr["c_id"]);
                         Session["UserName"] = TextBox1.Text;
                         Session["UserID"] = ID.Text;
    
                        //Response.Redirect("Fronpa.aspx");
    
                    }

    Not sure what you mean 1 or 11, i assume it's c_id that's exist in your query

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 12, 2018 12:41 AM

All replies

  • User-1716253493 posted
                    while (dr.Read())
                    {
                        //TextBox1.Text = Convert.ToString(dr["Email"]);
                        //TextBox2.Text = Convert.ToString(dr["password"]);
                        Label1.Text = Convert.ToString(dr["c_id"]);
                         Session["UserName"] = TextBox1.Text;
                         Session["UserID"] = ID.Text;
    
                        //Response.Redirect("Fronpa.aspx");
    
                    }

    Not sure what you mean 1 or 11, i assume it's c_id that's exist in your query

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 12, 2018 12:41 AM
  • User-1838255255 posted

    Hi Capricon User,

    According to your description and code, i notice that you only select the C_ID from the database, maybe you can not get the value of Email and password, like the following code: 

    TextBox1.Text = Convert.ToString(dr["Email"]); //maybe the dr["Email"] is null. 
    TextBox2.Text = Convert.ToString(dr["password"]);//maybe the dr["password"] is null. Session["UserName"] = TextBox1.Text; Session["UserID"] = ID.Text;
    // you could try the following code get ID value:
    string value=dr["C_ID"].tostring();
    label1.Text=value;

    Best Regards,

    Eric Du 

    Thursday, April 12, 2018 2:38 AM