Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# to Mysql Login code Error

C# to Mysql Login code Error

Scheduled Pinned Locked Moved C#
csharpdatabasemysqlhelpquestion
39 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N N Mohamed rafi

    I got error in this line sir cmd.ExecuteNonQuery();

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #5

    No, you get an error on the second call to Open. The debugger is just showing you the wrong line.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    N 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      No, you get an error on the second call to Open. The debugger is just showing you the wrong line.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      N Offline
      N Offline
      N Mohamed rafi
      wrote on last edited by
      #6

      Sir, I have removed First given open connection now the error is coming like MySql.Data.MySqlClient.MySqlException: 'Fatal error encountered during command execution.' MySqlException: Parameter '@loginid' must be defined.

      Richard DeemingR 1 Reply Last reply
      0
      • N N Mohamed rafi

        Sir, I have studied and tried to put login page but it shows code error please tell me the correction sir It shows error: System.InvalidOperationException: 'The connection is already and also code error private void button14_Click(object sender, EventArgs e) { if (textBox9.Text != "" && textBox10.Text != "") { string connectionString; MySqlConnection cnn; connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql"; cnn = new MySqlConnection(connectionString); cnn.Open(); string id = textBox9.Text; string password = textBox10.Text; textBox9.Text = ""; textBox10.Text = ""; string query = "select * from login where userid=@userid,password=@password,confirmpassword=@confirmpassword where loginid=@loginid is same"; //string query = "update employee set employee_name=@employee_name,employee_salary=@employee_salary where employee_id=@employee_id"; using (MySqlCommand cmd = new MySqlCommand(query)) { cmd.Parameters.AddWithValue("@userid", id); //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id)); cmd.Parameters.AddWithValue("@password", password); //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword); cmd.Connection = cnn; cnn.Open(); cmd.ExecuteNonQuery(); DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { MessageBox.Show("Login Successfully"); cnn.Close(); this.Hide(); Form2 f2 = new Form2(); f2.ShowDialog(); } else if (dr == DialogResult.No) { MessageBox.Show("Please Enter Correct Login details"); } } } else { MessageBox.Show("Please Enter details to Login"); } } }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        And you are still not checking the result of ExecuteNonQuery, but putting the success message based on the user pressing the "Yes" button.

        N 1 Reply Last reply
        0
        • L Lost User

          And you are still not checking the result of ExecuteNonQuery, but putting the success message based on the user pressing the "Yes" button.

          N Offline
          N Offline
          N Mohamed rafi
          wrote on last edited by
          #8

          How to check it. Please give step procedure sir or video link

          L 1 Reply Last reply
          0
          • N N Mohamed rafi

            Sir, I have removed First given open connection now the error is coming like MySql.Data.MySqlClient.MySqlException: 'Fatal error encountered during command execution.' MySqlException: Parameter '@loginid' must be defined.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #9

            Look at your query:

            Quote:

            select * from login where userid=@userid, password=@password, confirmpassword=@confirmpassword where loginid=@loginid is same

            You have two where clauses, which is not valid. And as far as I can see, that is same on the end is also not valid. And putting a comma between conditions is also not valid - you need to use AND instead. Aside from that, you have four parameters: @userid, @password, @confirmpassword, and @loginid. Now look at the parameters you are passing to the command:

            Quote:

            cmd.Parameters.AddWithValue("@userid", id);
            //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
            cmd.Parameters.AddWithValue("@password", password);
            //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);

            You are passing TWO parameters: @userid and @password. Either fix you query to use the parameters you are passing, or fix your code to pass the parameters required by the query.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            N 2 Replies Last reply
            0
            • N N Mohamed rafi

              How to check it. Please give step procedure sir or video link

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #10

              I have already explained more than once. When you call cmd.ExecuteNonQuery(); you must check the return value, to see whether the SQL command succeeded. Only when you have a success return can you post a message that tells the user that his action has worked. You must do this on all database access commands, never assume that your code has worked - most of the time it has not.

              N 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Look at your query:

                Quote:

                select * from login where userid=@userid, password=@password, confirmpassword=@confirmpassword where loginid=@loginid is same

                You have two where clauses, which is not valid. And as far as I can see, that is same on the end is also not valid. And putting a comma between conditions is also not valid - you need to use AND instead. Aside from that, you have four parameters: @userid, @password, @confirmpassword, and @loginid. Now look at the parameters you are passing to the command:

                Quote:

                cmd.Parameters.AddWithValue("@userid", id);
                //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
                cmd.Parameters.AddWithValue("@password", password);
                //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);

                You are passing TWO parameters: @userid and @password. Either fix you query to use the parameters you are passing, or fix your code to pass the parameters required by the query.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                N Offline
                N Offline
                N Mohamed rafi
                wrote on last edited by
                #11

                Sir, Now i have passed three parameters but it shows login successfull with wrong userid password also sir if (textBox9.Text != "" && textBox10.Text != "") { string connectionString; MySqlConnection cnn; connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql"; cnn = new MySqlConnection(connectionString); string id = textBox9.Text; string password = textBox10.Text; string loginid = ""; textBox9.Text = ""; textBox10.Text = ""; string query = "select * from login where userid=@userid and password=@password and loginid=@loginid"; using (MySqlCommand cmd = new MySqlCommand(query)) { cmd.Parameters.AddWithValue("@userid", id); cmd.Parameters.AddWithValue("@password", password); cmd.Parameters.AddWithValue("@loginid", loginid); cmd.Connection = cnn; cnn.Open(); cmd.ExecuteNonQuery(); DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { MessageBox.Show("Login Successfully"); cnn.Close(); this.Hide(); Form2 f2 = new Form2(); f2.ShowDialog(); } else if (dr == DialogResult.No) { MessageBox.Show("Please Enter Correct Login details"); } } } else { MessageBox.Show("Please Enter details to Login"); } }

                Richard DeemingR 1 Reply Last reply
                0
                • N N Mohamed rafi

                  Sir, Now i have passed three parameters but it shows login successfull with wrong userid password also sir if (textBox9.Text != "" && textBox10.Text != "") { string connectionString; MySqlConnection cnn; connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql"; cnn = new MySqlConnection(connectionString); string id = textBox9.Text; string password = textBox10.Text; string loginid = ""; textBox9.Text = ""; textBox10.Text = ""; string query = "select * from login where userid=@userid and password=@password and loginid=@loginid"; using (MySqlCommand cmd = new MySqlCommand(query)) { cmd.Parameters.AddWithValue("@userid", id); cmd.Parameters.AddWithValue("@password", password); cmd.Parameters.AddWithValue("@loginid", loginid); cmd.Connection = cnn; cnn.Open(); cmd.ExecuteNonQuery(); DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { MessageBox.Show("Login Successfully"); cnn.Close(); this.Hide(); Form2 f2 = new Form2(); f2.ShowDialog(); } else if (dr == DialogResult.No) { MessageBox.Show("Please Enter Correct Login details"); } } } else { MessageBox.Show("Please Enter details to Login"); } }

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #12

                  Because as the other Richard said below, you are not checking the result of your query! :doh: And as I said above, you are storing passwords insecurely. If you're intending to use this code in a real application, then I hope you've got deep pockets, because you're going to get hit with a multi-million dollar fine PDQ.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  N 1 Reply Last reply
                  0
                  • L Lost User

                    I have already explained more than once. When you call cmd.ExecuteNonQuery(); you must check the return value, to see whether the SQL command succeeded. Only when you have a success return can you post a message that tells the user that his action has worked. You must do this on all database access commands, never assume that your code has worked - most of the time it has not.

                    N Offline
                    N Offline
                    N Mohamed rafi
                    wrote on last edited by
                    #13

                    No sir mysql command was not succeeded, when i am giving wrong userid passwor dit shows login successfull i think sql query to be change

                    L 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Because as the other Richard said below, you are not checking the result of your query! :doh: And as I said above, you are storing passwords insecurely. If you're intending to use this code in a real application, then I hope you've got deep pockets, because you're going to get hit with a multi-million dollar fine PDQ.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      N Offline
                      N Offline
                      N Mohamed rafi
                      wrote on last edited by
                      #14

                      Sir, how to save password securely? pls suggest in my code and mysql query also wrong bcz it login with wrong userid and password also

                      Richard DeemingR 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        Look at your query:

                        Quote:

                        select * from login where userid=@userid, password=@password, confirmpassword=@confirmpassword where loginid=@loginid is same

                        You have two where clauses, which is not valid. And as far as I can see, that is same on the end is also not valid. And putting a comma between conditions is also not valid - you need to use AND instead. Aside from that, you have four parameters: @userid, @password, @confirmpassword, and @loginid. Now look at the parameters you are passing to the command:

                        Quote:

                        cmd.Parameters.AddWithValue("@userid", id);
                        //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
                        cmd.Parameters.AddWithValue("@password", password);
                        //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);

                        You are passing TWO parameters: @userid and @password. Either fix you query to use the parameters you are passing, or fix your code to pass the parameters required by the query.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        N Offline
                        N Offline
                        N Mohamed rafi
                        wrote on last edited by
                        #15

                        Sir, I have passed 3parameters but still it login in with wrong user id password also, how to write correct mysql query here?

                        string connectionString;
                        MySqlConnection cnn;
                        connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
                        cnn = new MySqlConnection(connectionString);
                        //cnn.Open();
                        string id = textBox9.Text;
                        string password = textBox10.Text;
                        string loginid = "";
                        textBox9.Text = "";
                        textBox10.Text = "";
                        string query = "select * from login where userid=@userid and password=@password and loginid=@loginid";
                        using (MySqlCommand cmd = new MySqlCommand(query))
                        {
                        cmd.Parameters.AddWithValue("@userid", id);
                        //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
                        cmd.Parameters.AddWithValue("@password", password);
                        cmd.Parameters.AddWithValue("@loginid", loginid);
                        //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);
                        cmd.Connection = cnn;
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo);
                        if (dr == DialogResult.Yes)
                        {
                        MessageBox.Show("Login Successfully");
                        cnn.Close();
                        this.Hide();
                        Form2 f2 = new Form2();
                        f2.ShowDialog();
                        }
                        else if (dr == DialogResult.No)
                        {
                        MessageBox.Show("Please Enter Correct Login details");
                        }
                        }
                        }
                        else
                        {
                        MessageBox.Show("Please Enter details to Login");
                        }
                        }

                        Richard DeemingR L 2 Replies Last reply
                        0
                        • N N Mohamed rafi

                          No sir mysql command was not succeeded, when i am giving wrong userid passwor dit shows login successfull i think sql query to be change

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #16

                          Yes, it shows login successful because, as I keep repeating, you post that message even when the ExecuteNonQuery fails. You need to start thinking about your code in logical steps rather than just throwing statements together and hoping it will work. 1. Perform the ExecuteNonQuery, and capture the return value. 2. Does the return value indicate success? 2.1. No - tell the user it failed. 2.2 Yes - and only at this point, tell the user it succeeded. 3. Perform other actions.

                          N 1 Reply Last reply
                          0
                          • N N Mohamed rafi

                            Sir, I have passed 3parameters but still it login in with wrong user id password also, how to write correct mysql query here?

                            string connectionString;
                            MySqlConnection cnn;
                            connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
                            cnn = new MySqlConnection(connectionString);
                            //cnn.Open();
                            string id = textBox9.Text;
                            string password = textBox10.Text;
                            string loginid = "";
                            textBox9.Text = "";
                            textBox10.Text = "";
                            string query = "select * from login where userid=@userid and password=@password and loginid=@loginid";
                            using (MySqlCommand cmd = new MySqlCommand(query))
                            {
                            cmd.Parameters.AddWithValue("@userid", id);
                            //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
                            cmd.Parameters.AddWithValue("@password", password);
                            cmd.Parameters.AddWithValue("@loginid", loginid);
                            //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);
                            cmd.Connection = cnn;
                            cnn.Open();
                            cmd.ExecuteNonQuery();
                            DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo);
                            if (dr == DialogResult.Yes)
                            {
                            MessageBox.Show("Login Successfully");
                            cnn.Close();
                            this.Hide();
                            Form2 f2 = new Form2();
                            f2.ShowDialog();
                            }
                            else if (dr == DialogResult.No)
                            {
                            MessageBox.Show("Please Enter Correct Login details");
                            }
                            }
                            }
                            else
                            {
                            MessageBox.Show("Please Enter details to Login");
                            }
                            }

                            Richard DeemingR Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #17

                            You really can't be bothered to pay attention, can you? :doh: Programming is not about throwing some random code together from a couple of internet searches, and then pestering other people to fix it for you. If you can't think for yourself, then you have chosen the wrong career.


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                            N 1 Reply Last reply
                            0
                            • N N Mohamed rafi

                              Sir, how to save password securely? pls suggest in my code and mysql query also wrong bcz it login with wrong userid and password also

                              Richard DeemingR Offline
                              Richard DeemingR Offline
                              Richard Deeming
                              wrote on last edited by
                              #18

                              One again, since you can't be bothered to pay attention: Secure Password Authentication Explained Simply[^] Salted Password Hashing - Doing it Right[^]


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                              1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                You really can't be bothered to pay attention, can you? :doh: Programming is not about throwing some random code together from a couple of internet searches, and then pestering other people to fix it for you. If you can't think for yourself, then you have chosen the wrong career.


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                N Offline
                                N Offline
                                N Mohamed rafi
                                wrote on last edited by
                                #19

                                Sir, I pay attention sir, you said i am passing 2parameters now i am passing 3parameters and login means it shows login successful with wrong userid password also i think my sql query should be change here

                                Richard DeemingR 1 Reply Last reply
                                0
                                • L Lost User

                                  Yes, it shows login successful because, as I keep repeating, you post that message even when the ExecuteNonQuery fails. You need to start thinking about your code in logical steps rather than just throwing statements together and hoping it will work. 1. Perform the ExecuteNonQuery, and capture the return value. 2. Does the return value indicate success? 2.1. No - tell the user it failed. 2.2 Yes - and only at this point, tell the user it succeeded. 3. Perform other actions.

                                  N Offline
                                  N Offline
                                  N Mohamed rafi
                                  wrote on last edited by
                                  #20

                                  I clearly telling again mysql query is error here, ExecuteNonQuery is returned value it means i logged in with wrong userid and password also.

                                  L 1 Reply Last reply
                                  0
                                  • N N Mohamed rafi

                                    I clearly telling again mysql query is error here, ExecuteNonQuery is returned value it means i logged in with wrong userid and password also.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #21

                                    No, that is not what it means, please read the documentation: SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient) | Microsoft Docs[^]. When you use a SELECT to find a particular user id and the return value says that there is an existing row it means that the details are correct. However since most of your code is in the wrong order it is unlikely that any of your results are correct.

                                    N 1 Reply Last reply
                                    0
                                    • N N Mohamed rafi

                                      Sir, I pay attention sir, you said i am passing 2parameters now i am passing 3parameters and login means it shows login successful with wrong userid password also i think my sql query should be change here

                                      Richard DeemingR Offline
                                      Richard DeemingR Offline
                                      Richard Deeming
                                      wrote on last edited by
                                      #22

                                      You are clearly not paying attention, neither here nor in your class. You have repeatedly been told that you need to check the results of your query. You have been told how to do that. And yet you continue to ask how to do what you have already been told how to do, and insist that you need to change your query rather than your code.


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                      1 Reply Last reply
                                      0
                                      • N N Mohamed rafi

                                        Sir, I have passed 3parameters but still it login in with wrong user id password also, how to write correct mysql query here?

                                        string connectionString;
                                        MySqlConnection cnn;
                                        connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
                                        cnn = new MySqlConnection(connectionString);
                                        //cnn.Open();
                                        string id = textBox9.Text;
                                        string password = textBox10.Text;
                                        string loginid = "";
                                        textBox9.Text = "";
                                        textBox10.Text = "";
                                        string query = "select * from login where userid=@userid and password=@password and loginid=@loginid";
                                        using (MySqlCommand cmd = new MySqlCommand(query))
                                        {
                                        cmd.Parameters.AddWithValue("@userid", id);
                                        //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
                                        cmd.Parameters.AddWithValue("@password", password);
                                        cmd.Parameters.AddWithValue("@loginid", loginid);
                                        //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);
                                        cmd.Connection = cnn;
                                        cnn.Open();
                                        cmd.ExecuteNonQuery();
                                        DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo);
                                        if (dr == DialogResult.Yes)
                                        {
                                        MessageBox.Show("Login Successfully");
                                        cnn.Close();
                                        this.Hide();
                                        Form2 f2 = new Form2();
                                        f2.ShowDialog();
                                        }
                                        else if (dr == DialogResult.No)
                                        {
                                        MessageBox.Show("Please Enter Correct Login details");
                                        }
                                        }
                                        }
                                        else
                                        {
                                        MessageBox.Show("Please Enter details to Login");
                                        }
                                        }

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #23

                                        How many times do I need to say it: Do not post a success message when you have not checked the result of your Database query. This is the way you should do it:

                                        cnn.Open();
                                        int result = cmd.ExecuteNonQuery(); // always capture the result
                                        cnn.Close();
                                        if (result == 1)
                                        {
                                        MessageBox.Show("Login was successful");
                                        }
                                        else
                                        {
                                        MessageBox.Show("The entered details were not correct");
                                        }

                                        Also, why do you need two ids (userid and loginid)? You should only need a single id and a password.

                                        N 3 Replies Last reply
                                        0
                                        • L Lost User

                                          How many times do I need to say it: Do not post a success message when you have not checked the result of your Database query. This is the way you should do it:

                                          cnn.Open();
                                          int result = cmd.ExecuteNonQuery(); // always capture the result
                                          cnn.Close();
                                          if (result == 1)
                                          {
                                          MessageBox.Show("Login was successful");
                                          }
                                          else
                                          {
                                          MessageBox.Show("The entered details were not correct");
                                          }

                                          Also, why do you need two ids (userid and loginid)? You should only need a single id and a password.

                                          N Offline
                                          N Offline
                                          N Mohamed rafi
                                          wrote on last edited by
                                          #24

                                          How to change code in my coding like this sir? because my code is logging in if i enter wrong username and password also

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups