1

This is my code I think I am doing right so far but I really dunno what is the problem. I am making a register form with txtbox for username and password I encrypt the password with MD5, I tried deleting the MD5 encryption thinking that it might be the prtoblem but still when I deleted it the problem is still occur.

ApareceCrudLib a = new ApareceCrudLib("localhost", "root", "",  "cashieringdb");

            string query = "INSERT INTO register (username,password) " +
                          "VALUES(" +
                           "'" + txtUser.Text + "'," +
                           "MD5('" + txtPass.Text +"')";
            a.mysqlInsert(query);
            MessageBox.Show("Account has been registered!");
            this.Close();

This is the code for my class ApareceCrudLib for mysqlInsert

 public void mysqlInsert(string query)
        {
            try
            {
                if (this.Open())
                {
                    MySqlCommand cmd = new MySqlCommand(query, conn);
                    cmd.ExecuteNonQuery();
                    this.Close();
                    System.Windows.Forms.MessageBox.Show("Record Inserted!");
                }
            }
            catch { this.Close(); System.Windows.Forms.MessageBox.Show("INSERT Record Error!"); }
            return;
        }

as you can see I catch the error with dialog box so basically if it will fail to insert or connect to database the message box shows "INSERT Record Error!". By the way there is no error in visual studio only in inserting to database.

I think the error somewhere in the code for inserting database string query = "INSERT

INTO register (username,password) " +
                              "VALUES(" +
                               "'" + txtUser.Text + "'," +
                               "MD5('" + txtPass.Text +"')";

maybe a comma a semi-colon a period I am clueless.

Hi!rhughes here is the image of the error!

enter image description here

5
  • as you can see I catch the error with dialog box so basically if it will fail to insert or connect to database the message box shows "INSERT Record Error!". By the way there is no error in visual studio only in inserting to database. Commented Mar 30, 2013 at 7:34
  • The answers are correct, and the exception message would have shown you that. Knowing there's an error isn't enough; you need to know what the error actually is. Commented Mar 30, 2013 at 7:36
  • Since you are willing to learn, you also might want to read about SQL Injection. You are using the input from the user directly in your sql statement this can be very dangerous Commented Mar 30, 2013 at 7:39
  • In regards to your edit, you're looking for an extra bracket on the end: "MD5('" + txtPass.Text +"'))"; Commented Mar 30, 2013 at 7:39
  • Thanks for the update. As @mdvlpr said, you are missing a ')' Commented Mar 30, 2013 at 7:45

3 Answers 3

3

you must add a ")" to your string query.

string query = "INSERT INTO register (username,password) " +
                      "VALUES(" +
                       "'" + txtUser.Text + "'," +
                       "MD5('" + txtPass.Text +"'))";
                                                  ^ HERE
Sign up to request clarification or add additional context in comments.

Comments

1

The SQL is not correct. You have two opening "(" and only one closing.

1 Comment

hahaha oh yes thank you how stupid I am I search every syntax for the error and forgot to notice the closing hahahhahahahaha
1

In order to see the actual error, try this:

try
{
    if (this.Open())
    {
        MySqlCommand cmd = new MySqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        this.Close();
        System.Windows.Forms.MessageBox.Show("Record Inserted!");
    }
}
catch(Exception ex)
{
    this.Close();
    System.Windows.Forms.MessageBox.Show(String.Format("INSERT Record Error! {0}", ex.Message));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.