0

I get a fatal error when I execute this code, but the info still shows up in the database. Can anyone see an error here?

sql.RunQuery("INSERT INTO test(test1,test2,test3) values(?FName,?LName,?DOB)")

            sql.SQLcmd.Parameters.AddWithValue("?FName", Gender.SelectedItem)
            sql.SQLcmd.Parameters.AddWithValue("?LName", Age)
            sql.SQLcmd.Parameters.AddWithValue("?DOB", RDIAge)

            sql.SQLcmd.ExecuteNonQuery()
2
  • 1
    I've gotten that a few times, but there was always a bit more information in the inner exception. Commented Jul 21, 2015 at 18:25
  • I'll keep digging. Thanks. Commented Jul 22, 2015 at 12:21

1 Answer 1

0

Your code should looks like:

  Dim connString As String = "Database=yourDB;Data Source=localhost;";
  connString +="User Id=yourUserDb;Password=dbPass" 

  Dim conn As New MySqlConnection(connString)
  Dim cmd As New MySqlCommand()
  Try
    conn.Open()
    cmd.Connection = conn

    cmd.CommandText = "INSERT INTO test(test1,test2,test3) values(@FName,@LName,@DOB)"
    cmd.Prepare()

    cmd.Parameters.AddWithValue("@FName", Gender.SelectedItem)
    cmd.Parameters.AddWithValue("@LName", Age)
    cmd.Parameters.AddWithValue("@DOB", RDIAge)
    cmd.ExecuteNonQuery()

    conn.Close()

  Catch ex As MySqlException
      Console.WriteLine("Error: " & ex.ToString())
  End Try
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! I figured it out about an hour ago. It was a problem with my SQLControl class (sql.). But essentially your way of doing it will also work. SO thanks for posting this :)
I'm glad to help @BrettEzra :)

Your Answer

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