Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Respected Sir,
i am using code for login page
default.aspx.cs
C#
try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from tbl_mnadmin where mn_username='"+txt_username.Text+"' and mn_password='"+txt_password.Text+"'",conn);
            SqlDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            if (rdr["mn_username"].ToString().Length > 0)
            {
                Session["username"] = rdr["mn_username"];
                Session["pass"] = rdr["mn_password"];
                Response.Redirect("welcome.aspx");
            }
            else
            {
                lbl_msg.Text = "Invalid User Name or Password";
            }

        }
        catch (Exception ex)
        {
        }
        finally
        {
            conn.Close();
        }

plz tell me problem. i have got a problem when username passowrd not match and if rdr was not filled. plz tell me how to solve this problem.
Thanks
REgards
Umesh Daiya
Posted
Updated 4-Oct-12 22:47pm
v2

Try like below..
C#
try
{
    conn.Open();
    SqlCommand cmd = new SqlCommand("select count(*) from tbl_mnadmin where mn_username='" +  txt_username.Text + "' and mn_password='" + txt_password.Text + "'", conn);
    int cnt=int.Parse(cmd.ExecuteScalar().ToString());

    if (cnt > 0)
    {
        Session["username"] = txt_username.Text;
        Session["pass"] = txt_password.Text;
        Response.Redirect("welcome.aspx");
     }
      else
        lbl_msg.Text = "Invalid User Name or Password";
}
catch (Exception ex)
{
}
finally
{
    conn.Close();
}
 
Share this answer
 
v2
To add to what Tejas says, for your own sake, don't do it like that!
Firstly:
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

For example, the way you do it, I do not need any password to log into your system - all I have to do is enter any username, followed by four other characters, and I am logged in. Or, I could extend that, log in (or not log in), and delete your entire database, from anywhere in the world.

Secondly:
Never store passwords in clear text! Have a look here: Password Storage: How to do it.[^]
 
Share this answer
 
I think you face this problem because you directly assessing your data from sqlreader with out checking weather there is data in your reader or not. so please modify your code to validate that part so, it might solve your problem. i think you can try something like this...

C#
try
{
    conn.Open();
    SqlCommand cmd = new SqlCommand("select * from tbl_mnadmin where mn_username='" + txt_username.Text + "' and mn_password='" + txt_password.Text + "'", conn);
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read()) // put validation like this, it will go forward only if there is some data in your reader.
    {
        if (rdr["mn_username"].ToString().Length > 0)
        {
            Session["username"] = rdr["mn_username"];
            Session["pass"] = rdr["mn_password"];
            Response.Redirect("welcome.aspx");
        }
        else
        {
            lbl_msg.Text = "Invalid User Name or Password";
        }
    }
}
catch (Exception ex)
{
}
finally
{
    conn.Close();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900