Im using a mysql connector to insert a record into a MYSQL Database table
The code i know works, because ive used it in an app before,
The poblem im getting is the error "object reference not set to an instance of an object" but i cant see what ive missed. I know it will be something so obvoius, so im hoping a fresh pair of eyes will find it.
Code:Private Sub SaveNewCustomer() 'Step 1 - Create SQL Command to INSERT record Dim cmd As MySqlCommand Dim cn As MySqlConnection cmd = New MySqlCommand("INSERT into CLIENTS (Company, Contact, Phone, Mobile, Email, Address, Website) VALUES (?Company, ?Contact, ?Phone, ?Mobile, ?Email, ?Address, ?Website)", cn) Try With cmd.Parameters '.AddWithValue("?ID", txtID.Text) .AddWithValue("?Company", txtCompany.Text) .AddWithValue("?Contact", txtContact.Text) .AddWithValue("?Phone", txtPhone.Text) .AddWithValue("?Mobile", txtMobile.Text) .AddWithValue("?Email", txtEmail.Text) .AddWithValue("?Address", txtAddress.Text) .AddWithValue("?Website", txtWWW.Text) End With Catch ex As Exception MsgBox(ex.Message) End Try 'Step 2 - Connect and Insert Try cn = New MySqlConnection() cn.ConnectionString = "server=" & My.Settings.HostIP & ";" & "user id=" & My.Settings.User & ";" & "password=" & My.Settings.Password & ";" & "database=clients" cn.Open() cmd.Connection = cn cmd.Prepare() cmd.ExecuteNonQuery() cn.Close() MsgBox("Successfully Added") Catch ex As Exception MsgBox(ex.Message) End Try End Sub




Reply With Quote
