0

hi, i m trying to make something like a login page and i used the following code to compare the Users password from a password text box and then compare it with the Database password to see if it is the same. nevertheless when i compare the strings the system doesnt accept them as the same and i cant login.(i also tried the String.Compare but i get the same resuly). Any help pleasE?

String username = txtusername.Text;
        String password = txtpass.Text;
        String connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select password from Details WHERE username='" + username + "';";
        Console.WriteLine(cmd.CommandText);
        conn.Open();
        String pass = cmd.ExecuteScalar().ToString();
        conn.Close();
        if (pass==password)
        {
            Label1.Text = "Y";
        }
        else
        {
            Label1.Text="N";
        }
2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by pikkas
0

pikkas,

Is this web (ASP.NET) or win app?

Try this,

String username = txtusername.Text;
        String password = txtpass.Text;
        String connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select password from Details WHERE username=@p1";
        cmd.Parameters.AddWithValue("@p1",username);
       
        conn.Open();
        object pass = cmd.ExecuteScalar();
        conn.Close();
        
        if (pass!=null)
        {
          if(pass.ToString()==password)
               Label1.Text = "Y";
          else
             Label1.Text="N";
        }
        else
            Label1.Text="N";
0

Yep, its a web app. Thx for your reply but it doesnt work again :(

pikkas,

Is this web (ASP.NET) or win app?

Try this,

String username = txtusername.Text;
        String password = txtpass.Text;
        String connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select password from Details WHERE username=@p1";
        cmd.Parameters.AddWithValue("@p1",username);
       
        conn.Open();
        object pass = cmd.ExecuteScalar();
        conn.Close();
        
        if (pass!=null)
        {
          if(pass.ToString()==password)
               Label1.Text = "Y";
          else
             Label1.Text="N";
        }
        else
            Label1.Text="N";
0

Did you got errors? Please add break point (VS2005/8/10) and debug your app. Also try this select statement.

cmd.CommandText = "select [password] from [Details] WHERE [username]=@p1";
0

The problem is not the sql statement. The problem is the pass==password. I have the same passwords but the app doesnt recognize them as same. i also tried the string.compare but still doesnt work

Did you got errors? Please add break point (VS2005/8/10) and debug your app. Also try this select statement.

cmd.CommandText = "select [password] from [Details] WHERE [username]=@p1";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.