Click here to Skip to main content
13,150,946 members (46,009 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi friends;
i am having a problem working with my loging form which i design in winform here is my code
  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 18-Nov-12 11:44am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
if ( dr.hasRows )
  messageBox.Show("Login Successful");
else
  messageBox.Show("LOGIN NOT SUCCESSFUL");
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 &&
  Permalink  
Comments
Ese Ochuko 18-Nov-12 18:25pm
   
Still not working
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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 );
 
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100