Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
MySqlConnection connection;
                string userid = string.Empty;
                string password = string.Empty;
                string connectionString = string.Empty;
                string query = string.Empty; //"INSERT INTO tableinfo (name, age) VALUES('John Smith', '33')";
                userid = UserIDCtrl.Text;
                password = PasswordCtrl.Text;

                //Check empty userid && Password
                CheckForUserIdPasswordEmpty(userid, password);
                 
                
                connectionString = "SERVER=localhost;" + "DATABASE=AshwiniDB;" + "UID=root;" + "PASSWORD=root;"+"PORT=3306;";

                connection = new MySqlConnection(connectionString);

                connection.Open();

                query = "SELECT * FROM logintable WHERE  USERID ='" + userid + "' AND  PASSWORD ='" + password + "'";
                
                

                MySqlCommand cmd = new MySqlCommand(query, connection );

                cmd.ExecuteNonQuery();

                MySqlDataReader dataReader = cmd.ExecuteReader();

                if (dataReader.Read())
                {
                    MessageBox.Show("Login Successful");
                }
                else { MessageBox.Show("Please Enter valid USERID and PASSWORD"); }


                connection.Close();


            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
Posted
Updated 18-Aug-14 18:46pm
v2
Comments
Ashi0891 19-Aug-14 0:49am    
try removing the statement "cmd.ExecuteNonQuery();"
SRS(The Coder) 19-Aug-14 1:22am    
Are you getting any error ?
Sanchayeeta 19-Aug-14 1:29am    
I have run this code. I didn't get any error, Even I am getting data also.
I would suggest you to debug it properly. When query string formed, If possible check manually in DB is it returning any value or not.

you can do as below

C#
if(dataReader.Read())
{
    string res = dataReader.GetString(0);
    //...
}


Refer Mysql Documentation for more details
MySqlCommand[^]
 
Share this answer
 
v2
i suppose you are looking for authentication function..



C#
SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"); 
    string sql = "SELECT * FROM TableName WHERE UserID = @username AND Password = @password"; 
    SqlCommand cmd = new SqlCommand(sql,connection); 
    cmd.Parameters.AddWithValue("@username", user); 
    cmd.Parameters.AddWithValue("@password", pass); 
    connection.Open(); 
  
    DataTable dt = new DataTable(); 
    SqlDataAdapter ad = new SqlDataAdapter(cmd); 
    ad.Fill(dt); 
    if (dt.Rows.Count > 0) { //check if the query returns any data 
        //Valid Username and Password 
        Response.Redirect("Default.aspx"); 
    } 
    else 
    { 
        Response.Write("INVALID Username and Password, Try Again!"); 
    } 
    connection.Close();    
 
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