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";
        }

Recommended Answers

All 4 Replies

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";

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";

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";

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";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.