Results 1 to 29 of 29

Thread: object reference not set to an instance of an object

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Angry object reference not set to an instance of an object

    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
    Last edited by mjenkinson05; Sep 7th, 2009 at 05:19 AM. Reason: More Relevent Title

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width