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

    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
      • 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
        #25

        Put the same code in my code that why i can understand easily sir

        1 Reply 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
          #26

          I have changed my code sir, but still logging in for wrong userid and password

          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;
          textBox9.Text = "";
          textBox10.Text = "";
          string query = "select * from login where userid=@userid and password=@password";
          using (MySqlCommand cmd = new MySqlCommand(query))
          {
          cmd.Parameters.AddWithValue("@userid", id);
          cmd.Parameters.AddWithValue("@password", password);
          cmd.Connection = cnn;
          cnn.Open();
          cmd.ExecuteNonQuery();
          DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation Message", 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 With Correct Login Details");
          }

          L 1 Reply Last reply
          0
          • N N Mohamed rafi

            I have changed my code sir, but still logging in for wrong userid and password

            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;
            textBox9.Text = "";
            textBox10.Text = "";
            string query = "select * from login where userid=@userid and password=@password";
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
            cmd.Parameters.AddWithValue("@userid", id);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Connection = cnn;
            cnn.Open();
            cmd.ExecuteNonQuery();
            DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation Message", 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 With Correct Login Details");
            }

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

            There really is no point in continuing this thread. It does not matter how many times we tell you how to correct your code you insist on ignoring our advice. My previous reply showed you exactly how to do it correctly, and yet you just repeat the same bad code. I suggest you consider a different career path.

            N 2 Replies Last reply
            0
            • L Lost User

              There really is no point in continuing this thread. It does not matter how many times we tell you how to correct your code you insist on ignoring our advice. My previous reply showed you exactly how to do it correctly, and yet you just repeat the same bad code. I suggest you consider a different career path.

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

              Why you are not correcting my code and sending me? i said the error you know the code and sending some examples also then you can send correct code it is useful to me please

              L 1 Reply Last reply
              0
              • N N Mohamed rafi

                Why you are not correcting my code and sending me? i said the error you know the code and sending some examples also then you can send correct code it is useful to me please

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

                I gave you the correct code in my message above at Re: C# to Mysql Login code Error - C# Discussion Boards[^]. Why don't you actually go and read it, and better still, use it to correct your own code?

                1 Reply Last reply
                0
                • L Lost User

                  There really is no point in continuing this thread. It does not matter how many times we tell you how to correct your code you insist on ignoring our advice. My previous reply showed you exactly how to do it correctly, and yet you just repeat the same bad code. I suggest you consider a different career path.

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

                  Sir, How to write mysql query if stored userid password is correct means then it enter successfull suppose not stored values entered means the messagebox is wrong.....

                  L 1 Reply Last reply
                  0
                  • N N Mohamed rafi

                    Sir, How to write mysql query if stored userid password is correct means then it enter successfull suppose not stored values entered means the messagebox is wrong.....

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

                    Why do you ignore everything we tell you? Go back to the code sample I gave you, it shows exactly how to do it correctly. This is my last message on this subject.

                    N 1 Reply Last reply
                    0
                    • L Lost User

                      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 Offline
                      N Offline
                      N Mohamed rafi
                      wrote on last edited by
                      #32

                      Sir, You all are blaming me always, you know the code but you are not providing me. Thank you all.

                      D 1 Reply Last reply
                      0
                      • L Lost User

                        Why do you ignore everything we tell you? Go back to the code sample I gave you, it shows exactly how to do it correctly. This is my last message on this subject.

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

                        Pls correct and send me in my code sir that why i can understand sir send me please

                        D 1 Reply Last reply
                        0
                        • N N Mohamed rafi

                          Sir, You all are blaming me always, you know the code but you are not providing me. Thank you all.

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #34

                          I am so glad I didn't answer this question last night. You're not trying to think about the problem. You're not trying to read the documentation on the methods you're calling. You're not trying to read the links other people have given you to educate you on what you should be doing. You're not trying to work out what you should be doing, logically, step-by-step. You're not trying to understand anything. What you ARE trying to do, throughout this entire thread, is get other people to write your code for you, and that will teach you nothing.

                          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                          Dave Kreskowiak

                          N 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            I am so glad I didn't answer this question last night. You're not trying to think about the problem. You're not trying to read the documentation on the methods you're calling. You're not trying to read the links other people have given you to educate you on what you should be doing. You're not trying to work out what you should be doing, logically, step-by-step. You're not trying to understand anything. What you ARE trying to do, throughout this entire thread, is get other people to write your code for you, and that will teach you nothing.

                            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                            Dave Kreskowiak

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

                            Sir, Now you give the right code or not?

                            D 1 Reply Last reply
                            0
                            • N N Mohamed rafi

                              Sir, Now you give the right code or not?

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #36

                              See, you're STILL trying to get other people to do your work for you. The answer to that very question, from EVERYONE, is going to be NO. The only code you get is the code YOU write.

                              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                              Dave Kreskowiak

                              N 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                See, you're STILL trying to get other people to do your work for you. The answer to that very question, from EVERYONE, is going to be NO. The only code you get is the code YOU write.

                                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                Dave Kreskowiak

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

                                Waste of time in this forum. Bye

                                D 1 Reply Last reply
                                0
                                • N N Mohamed rafi

                                  Waste of time in this forum. Bye

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #38

                                  It's a waste of time only because YOU made it that way by begging other people to write your code for you.

                                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                  Dave Kreskowiak

                                  1 Reply Last reply
                                  0
                                  • N N Mohamed rafi

                                    Pls correct and send me in my code sir that why i can understand sir send me please

                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #39

                                    How many times do you have to be told "nobody is going to do your work for you" before it sinks in?

                                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                    Dave Kreskowiak

                                    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