Click here to Skip to main content
11,495,743 members (58,028 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WinForm
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....

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 7-Mar-13 1:52am
Edited 7-Mar-13 2:05am
v3
Comments
ridoy at 7-Mar-13 6:56am
   
what mistakes?clear your question..
GopinathSk at 7-Mar-13 7:01am
   
else part is not working......
ryanb31 at 7-Mar-13 6:56am
   
What do you need?
GopinathSk at 7-Mar-13 6:58am
   
else part is not working......
ryanb31 at 7-Mar-13 7:01am
   
And what does that mean?
GopinathSk at 7-Mar-13 7:11am
   
this is simple login form....i want,if the user give wrong crediential then it must go for else part...But actually it not goto else....
ryanb31 at 7-Mar-13 7:15am
   
So, that must mean that this.CompareStrings(sdr["password"].ToString(), pwd) && this.CompareStrings(sdr["username"].ToString(), user) is returning true. Just focus on that.
GopinathSk at 7-Mar-13 7:22am
   
yeah, it correct but if i give wrong one means it doesn't show an error msg
ryanb31 at 7-Mar-13 7:28am
   
Simply putting breakpoints in your code and stepping through it would have found the issue. See solution 1.
GopinathSk at 7-Mar-13 7:01am
   
else part is not working......
phil.o at 7-Mar-13 6:58am
   
MessageBox.Show("Error");

This is the only code in your 'else' part.
What is the error exactly ? Are we supposed to guess ?
GopinathSk at 7-Mar-13 7:00am
   
i want like that..
if the user give wrong crediantial then else part will execute
phil.o at 7-Mar-13 7:02am
   
Sorry, but it still does not answer the question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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):

            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
  Permalink  
v2
Comments
Surendra0x2 at 7-Mar-13 7:32am
   
:D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

by mistake i put my else part inside the while loop..that's the problem....


here the solution is::
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");
           }
  Permalink  

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

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 CHill60 318
2 Sascha Lefèvre 275
3 RyanDev 230
4 Sergey Alexandrovich Kryukov 210
0 Sergey Alexandrovich Kryukov 10,362
1 OriginalGriff 8,821
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 7 Mar 2013
Copyright © CodeProject, 1999-2015
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