1

i am learning asp.net with c# by myself, and i have a problem with DataRows, in db i have users table and there is isadmin column which value is int, i want to redirect users to different page and admins to admin page, but the problem is all users redirects to admin page.

Here is my code;

 protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(conString);
        conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT username, pass FROM users 
                                where username = '"+txtUser.Text+"'
                                and pass='"+txtPass.Text+"'"
                                , conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        SqlCommand cmd1 = new SqlCommand("Select username, isadmin From users", conn);
        SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);

        conn.Close();
        if (dt.Rows.Count > 0)
        {
            Session["id"] = txtUser.Text;
            if (dt1.Rows[0]["isadmin"].ToString() == "1")
            {
                Response.Redirect("~/admin.aspx");
            }
            else
            {
                Response.Redirect("~/default.aspx");
            }


            //Response.Redirect("~/default.aspx");

            Session.RemoveAll();
        }
        else
        {
            lblMsg.ForeColor = System.Drawing.Color.Red;
            //lblMsg.Text= msg ;

                /*Response.Write("<script>
                alert('Please enter valid Username and Password')
                </script>"); */
        }

Can you please tell me what is wrong?

13
  • 1
    why do you have 2 different query that queries to same table? Commented Jan 19, 2017 at 8:29
  • seems like all users which you are fetching have isadmin column value 1. Commented Jan 19, 2017 at 8:29
  • hi have you tried have you looked into data of the database . Also can you print the value of dt1.Rows[0]["Isadmin"] to see what is output ? Commented Jan 19, 2017 at 8:29
  • 1
    Always use parametrized queries not string concatenation to build your sql qeueries. Otherwise you are open for sql injection(among other issues). Commented Jan 19, 2017 at 8:32
  • 1
    dt is filled via "SELECT username, pass" and with this does NOT contain IsAdmin. The query for dt has to be extended by the IsAdmin column. Commented Jan 19, 2017 at 8:36

5 Answers 5

3

Use the first query with dt as it's based on a single user. The problem is dt1 gets all users and the first record in that datatable is an admin

if (dt.Rows[0]["isadmin"].ToString() == "1") {

Remove the second query with dt1 and make sure you add isadmin to the first SQL query.

SqlCommand cmd = new SqlCommand("SELECT username, pass, isadmin FROM users where username = @UserName and pass= @Pass", conn); 

See how I use parameterized username and password, that is to protect against SQL injection, definitely read up on that!!!

Sign up to request clarification or add additional context in comments.

2 Comments

how can i try to get isadmin value from dt?
Add the IsAdmin field to the first SQL query, I gave you all the reasons this doesn't work to try and teach you so you can learn to work this out yourself in future, Zohar gave you a copy/paste code answer that gives it to you without any effort. Give a man a fish, feed him for a day, teach a man how to fish and feed him for life. When debugging you have to go after the bug like a dog fetching a bone, good luck mate
0

Please Try this

protected void btnLogin_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(conString);
    conn.Open();
    SqlCommand cmd =
        new SqlCommand(
            "SELECT username, pass, isadmin FROM users where username = '" + txtUser.Text + "' and pass='" + txtPass.Text +
            "'", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    conn.Close();
    if (dt.Rows.Count > 0)
    {
        Session["id"] = txtUser.Text;
        if (dt.Rows[0]["isadmin"].ToString() == "1")
        {
            Response.Redirect("~/admin.aspx");
        }
        else
        {
            Response.Redirect("~/default.aspx");
        }


        //Response.Redirect("~/default.aspx");

        Session.RemoveAll();
    }
    else
    {
        lblMsg.ForeColor = System.Drawing.Color.Red;
        //lblMsg.Text= msg ;

        //Response.Write("<script>alert('Please enter valid Username and Password')</script>");
    }
}

Comments

0

In your first query you need to get isadmin also and on the base of that result you can check either it is 1 or not and can redirect to what ever page you like. So it will be as follow:

protected void btnLogin_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(conString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT username, pass, isadmin FROM users where username = '"+txtUser.Text+"' and pass='"+txtPass.Text+"'", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    conn.Close();
    if (dt.Rows.Count > 0)
    {
        Session["id"] = txtUser.Text;
        if (dt.Rows[0]["isadmin"].ToString() == "1")
        {
            Response.Redirect("~/admin.aspx");
        }
        else
        {
            Response.Redirect("~/default.aspx");
        }
        //Response.Redirect("~/default.aspx");
        Session.RemoveAll();
    }
    else
    {
        lblMsg.ForeColor = System.Drawing.Color.Red;
        //lblMsg.Text= msg ;
        //Response.Write("<script>alert('Please enter valid Username and Password')</script>");
    }
}

Comments

0

There are several things wrong with your code:

  1. All users are redirected to the admin page since you are checking the isAdmin in the wrong query. Your second query has no where clause which means it will return all the users in the table. The first user it returns has the isAdmin value of 1.
    You don't actually need two queries, just one.

  2. You must use parameterized queries, otherwise you are leaving an open door to SQL injection attacks.

  3. wrap all IDisposable instances in a using statement.

Your code should look more like this:

protected void btnLogin_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    using(SqlConnection conn = new SqlConnection(conString))
    {
        using(SqlCommand cmd = new SqlCommand("SELECT username, pass, isadmin FROM users where username = @UserName and pass=@Pass", conn))
        {
            cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUser.Text;
            cmd.Parameters.Add("@Pass", SqlDbType.VarChar).Value = txtPass.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
        }   

    }
    if (dt.Rows.Count > 0)
    {
        Session["id"] = txtUser.Text;
        if (dt1.Rows[0]["isadmin"].ToString() == "1")
        {
            Response.Redirect("~/admin.aspx");
        }
        else
        {
            Response.Redirect("~/default.aspx");
        }


        //Response.Redirect("~/default.aspx");

        Session.RemoveAll();
    }
    else
    {
        lblMsg.ForeColor = System.Drawing.Color.Red;
        //lblMsg.Text= msg ;

        //Response.Write("<script>alert('Please enter valid Username and Password')</script>");
    }

}

4 Comments

this should be a comment not an answer
You are right value is always 1, i was trying many things to solve the problem, this is my last try, i will try to parametrize the query to avoid sql injection.
your coding is complicated for me, no doubt you are professional, can you give me a link which explains "wrap all IDisposable instances in a using statement.". thank you for your help
The using in my answer is also a link to the relevant page in MSDN...
-1

Your second query lacks the filter on a user name:

Select username, isadmin From users

So whatever it fetches - if the first row contains 1 as IsAdmin, all users will be redirected to the admin page.

3 Comments

dt does not contain the column IsAdmin
i have tried like this; Select isadmin From users but no hope!
You should try SELECT username, pass, isadmin FROM users in the first cmd

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.