Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
if part works very well .But the else part has mistakes in it..(what wrong in else part)...

this is my code...please assist me....

C#
SqlConnection con = new SqlConnection(@"Data source=LENOVO-PC\SQLEXPRESS1;Initial catalog=bank;user id=sa;password=123456");
            con.Open();
            SqlCommand cmd = new SqlCommand("select username ,password from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'", con);
            SqlDataReader sdr = cmd.ExecuteReader();

            string user = textBox1.Text;
            string pwd = textBox2.Text;
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {

                    if (this.CompareStrings(sdr["password"].ToString(), pwd) && this.CompareStrings(sdr["username"].ToString(), user))
                    {
                        MessageBox.Show("Login");

                    }

                    else
                    {
                        MessageBox.Show("Error");
                    }

                }
            }
            sdr.Close();
            con.Close();
Posted
Updated 7-Mar-13 1:05am
v3
Comments
ridoy 7-Mar-13 6:56am    
what mistakes?clear your question..
GopinathSk 7-Mar-13 7:01am    
else part is not working......
ZurdoDev 7-Mar-13 6:56am    
What do you need?
GopinathSk 7-Mar-13 6:58am    
else part is not working......
ZurdoDev 7-Mar-13 7:01am    
And what does that mean?

You'll probably laugh yourself silly when I point it out to you, but here I go anyway:

The "else" part will of course never execute, because the SQL select statement will only return a row from the table login if both the username and the password match.
Since there will only be a row if and when they match you can rewrite your code like this (only the outer if is nescessary):

C#
if (sdr.HasRows) // if it has a row then username and password matched
{
    MessageBox.Show("Login");
}
else             // if there is no row they didn't match
{
    MessageBox.Show("Error");
}


If your datamodel is correct you don't even have to check if there are more than one row.
Please also observe this: Never construct your SQL statements by concatenating strings, use SqlParameters instead.

Regards,
— Manfred
 
Share this answer
 
v2
Comments
Surendra0x2 7-Mar-13 7:32am    
:D
by mistake i put my else part inside the while loop..that's the problem....


here the solution is::
C#
if (sdr.HasRows)
           {
               while (sdr.Read())
               {

                   if (this.CompareStrings(sdr["username"].ToString(), user) && this.CompareStrings(sdr["password"].ToString(), pwd))
                   {
                       MessageBox.Show("Login");
                   }


               }
           }
           else
           {
               MessageBox.Show("Error");
           }
 
Share this answer
 

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