0

This is not working at all. I have done it many times but I don't know what's going wrong. the textbox always shows "not found" whereas it should be showing username. Note: Textbox is just an example.

Login Page:

     protected void login_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Session.Abandon();
        Session.Clear();
        string username = email.Text.ToLower().Trim();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
        con.Open();
        string sql = "SELECT USERNAME, PASSWORD FROM MANAGER WHERE USERNAME = @USERNAME AND PASSWORD=@PASSWORD";
        SqlCommand command = new SqlCommand(sql, con);
        command.Parameters.AddWithValue("@USERNAME", username);
        command.Parameters.AddWithValue("@PASSWORD", password.Text.Trim());
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        da.Fill(dt);
        command.ExecuteNonQuery();
        command.Dispose();
        con.Close();
        if (dt.Rows.Count > 0)
        {
            Session["manager"] = username;
            Response.Redirect("ManagerHomePage.aspx");
            Session.RemoveAll();
        }
        else
        {
            Label1.Text = "Invalid Email or Password!";
        }
    }

ManagerHomePage.aspx : protected void Page_Load(object sender, EventArgs e) {

        if(Session["manager"]!=null)
        {
            TextBox1.Text = Session["manager"].ToString();

        }
        else
        {
            TextBox1.Text = "not found";
        }

    }
1
  • I removed Session.RemoveAll(), Session.Abandon(), Session.Clear() from the first 3 lines of Sign In Button and it works fine. Commented Jul 30, 2020 at 4:32

1 Answer 1

0

Do not use ExecuteNonQuery for Select command. ExecuteNonQuery is used for Insert, Update, Delete command. Try using ExecuteReader Bellow is the code

protected void login_Click(object sender, EventArgs e)
{
    Session.RemoveAll();
    Session.Abandon();
    Session.Clear();
    string username = email.Text.ToLower().Trim();
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
    con.Open();
    string sql = "SELECT USERNAME, PASSWORD FROM MANAGER WHERE USERNAME = @USERNAME AND PASSWORD=@PASSWORD";
    SqlCommand command = new SqlCommand(sql, con);
    command.Parameters.AddWithValue("@USERNAME", username);
    command.Parameters.AddWithValue("@PASSWORD", password.Text.Trim());
    SqlDataReader rdr=command.ExecuteReader();
    if(rdr.HasRows())
    {
        Session["manager"] = username;
        Response.Redirect("ManagerHomePage.aspx");
    }        
    else
    {
        Label1.Text = "Invalid Email or Password!";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.