mardi 30 octobre 2018

Piece of code not working correctly for Admin Login

I've just finished my login page, Ive made it so that it redirects an ordinary User to the Home Page while an Admin to a main page with navigation for Administrative functions. The issue is that my If statement runs and redirects back to "Home" even when the administrator credentials are entered. I'm not sure how to stop this, here is the code:

public partial class LogIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection
            (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TennisTimeDTB.mdf;Integrated Security=True;");
        SqlCommand cmd = new SqlCommand("select * from Login where username=@username and password=@password", con);
        cmd.Parameters.AddWithValue("@username", TextBox1.Text);
        cmd.Parameters.AddWithValue("@password", TextBox2.Text);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();

        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["Role"].ToString() == "admin")
            {

                Response.Redirect("AdminMain.aspx");

            }
            else
            {
                Session["id"] = TextBox1.Text;
                Response.Redirect("Home.aspx");
                Session.RemoveAll();
            }
        }

I'm pretty fresh to this so if it's a simple mistake I apologise! Thanks in advance for any help!

Aucun commentaire:

Enregistrer un commentaire