Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string MyconString = "SERVER=localhost;" +
                                         "DATABASE=record;" +
                                         "UID=testuser;" +
                                         "PASSWORD=testpaswd;";
           MySqlConnection connection = new MySqlConnection(MyconString);
           MySqlCommand command = connection.CreateCommand();

           // command.CommandText = "SELECT * FROM users";
           command.CommandText = "SELECT DISTINCT * FROM users WHERE name='" +
                             data[0] + "'AND password='" + data[1] + "'";

           MySqlDataReader myreader = null;
           try
           {
               connection.Open();
               myreader = command.ExecuteReader();
               while (myreader.Read())
               {
                   string name_stored = (myreader["name"].ToString());
                   string paswd_stored = (myreader["password"].ToString());
                   string camID_stored = (myreader["cam_ID"].ToString());
                 
                   if (data[0].Equals(name_stored) && data[1].Equals(paswd_stored))
                   {
                       Console.WriteLine("Log in successfull");  
                   }
                else
                   {
                       Console.WriteLine("Log in Failed. . . ");
                   }                                  
               }
               connection.Close();
           }           
           catch(Exception eo)
               {
                   Console.WriteLine(eo.ToString());
               
               }

Q: This is login module code for my application. problem is that when i enter a incorrect user name and password it gets stuck and else condition for wrong user name and password do not execute.
Posted
Updated 24-Mar-11 4:18am
v2
Comments
Sandeep Mewara 24-Mar-11 10:24am    
Why not? All looks ok. Just a Console.WriteLine. Did you debug?
Tarun.K.S 24-Mar-11 10:36am    
Its a simple mistake that OP made. Look at the command.
Sandeep Mewara 24-Mar-11 10:37am    
Thanks. :)

BTW, This simply means he did not DEBUG even once! :doh:
Tarun.K.S 24-Mar-11 10:38am    
True!

The problem is that the data reader will come back empty if the username or password entered are not in the database. Therefore your while loop will not execute at all.
 
Share this answer
 
Comments
Dalek Dave 24-Mar-11 10:35am    
Spot on.
#realJSOP 24-Mar-11 10:45am    
Proposed as answer
Well I'm not surprised: you only retrieve records from the database which match you login name and password. So, if you enter a bad password, it will retrieve no records, and will not enter the while loop to check anything.

BTW: don't access your DB like that: you are leaving yourself wide open to an SQL Injection attack. Use parametrized queries instead:
C#
command.CommandText = "SELECT DISTINCT * FROM users WHERE name=@NM AND password=@PW";
command.Parameters.AddWithValue("@NM", data[0]);
command.Parameters.AddWithValue("@PW", data[1]);
 
Share this answer
 
The problem lies in the Command :

SQL
command.CommandText = "SELECT DISTINCT * FROM users WHERE name='" +
                             data[0] + "'AND password='" + data[1] + "'";


Remove the WHERE condition, so that the command can retrieve all the Names and passwords. Then in the while loop, you can compare data[0] with name and data[1] with password and not in the command.

Just put the command simply like this :

SQL
command.CommandText = "SELECT DISTINCT * FROM users;


Hope it helped!
 
Share this answer
 
v2
Try If(myReader.read) in the place of While(myReader.read).
 
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