Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi friends;
i am having a problem working with my loging form which i design in winform here is my code
C#
private void btnLogin_Click(object sender, EventArgs e)
      {
          user = txtUsername.Text;
          pass = txtPassword.Text;
          role = cbRole.Text;
          SqlConnection con = new SqlConnection();
          con.ConnectionString = "Data Source=ESE-HP;Initial Catalog=PIMS;User ID=sa;PWD=ese";
          con.Open();
          SqlCommand cmd = new SqlCommand("select * from employee where  occupation = '" + role + "' and password = '" + pass + "' and username = '" + user + "' ", con);
          SqlDataReader dr = cmd.ExecuteReader();


          while (dr.Read())
          {
              if ((dr["Password"].ToString() == pass && dr["Username"].ToString() == user && dr["Occupation"].ToString() == role))
              {
                  messageBox.Show("Login Successful");
              }

             else
              {
                  MessageBox.Show("LOGIN NOT SUCCESSFUL );
              }
            }
      }


i have a login form that has username, password and role textbox when i run the code and enter my correct credentials, it displays the message "LOGIN SUCCESSFUL", but if i did not enter the correct information in the boxes, it wont display the 'else' message which is "LOGIN NOT SUCCESSFUL", i do not know what the problem is, can someone help me out? thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Nov-12 18:09pm    
Do you use the debugger? This is what you always need before asking such questions, and in fact, usually -- instead of. :-)
--SA

Try this:

private void btnLogin_Click(object sender, EventArgs e)
{
            user = txtUsername.Text;
            pass = txtPassword.Text;
            role = cbRole.Text;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=ESE-HP;Initial Catalog=PIMS;User ID=sa;PWD=ese";
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from employee where  occupation = '" + role + "' and password = '" + pass + "' and username = '" + user + "' ", con);
            SqlDataReader dr = cmd.ExecuteReader();
 

if (dr != null)messageBox.Show("Login Successful");
else MessageBox.Show("LOGIN NOT SUCCESSFUL );
 
}
 
Share this answer
 
You are redundantly checking the passed credentials: the first time with the SQL query, the second time with code inside the loop.
You might change
Quote:
while (dr.Read())
{
if ((dr["Password"].ToString() == pass && dr["Username"].ToString() == user && dr["Occupation"].ToString() == role))
{
messageBox.Show("Login Successful");
}

else
{
MessageBox.Show("LOGIN NOT SUCCESSFUL );
}
}

to
C#
if ( dr.hasRows )
  messageBox.Show("Login Successful");
else
  messageBox.Show("LOGIN NOT SUCCESSFUL");
 
Share this answer
 
v2
Comments
Nelek 18-Nov-12 18:28pm    
The magic of simplicity. +5
CPallini 19-Nov-12 3:58am    
Thank you.
Ese Ochuko 22-Nov-12 1:15am    
wonderful!!!!!!!-------..
thank you very much
CPallini 22-Nov-12 3:32am    
You are welcome.
Try this
use try catch block, and get the database credentials informations in variable before compare them to the user input and try to use & rather than &&
 
Share this answer
 
Comments
Ese Ochuko 18-Nov-12 18:25pm    
Still not working

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