first, i am sorry if i can't speak english well

i have problem where i can't insert data to data base mySQl using vb2005.
it is my simple code on vb.

----------------------------------

Imports MySql.Data
Imports MySql.Data.MySqlClient

Public Class Form1
    Dim xobjConnection As New MySqlConnection("Server=localhost;uid=root;pwd=;database=test1;")

    Dim xobjAdapter As New MySqlDataAdapter()
    Dim xobjDataSet As DataSet


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim nim As String
        Dim nama As String

        nim = Me.TextBox1.Text
        nama = Me.TextBox2.Text

        Dim xobjCommand As MySqlCommand = New MySqlCommand()

        xobjCommand.Connection = Me.xobjConnection
        xobjCommand.CommandText = "INSERT INTO apple " & _
        "( nama, nim ) " & _
        "VALUES ( @nama, @nim)"

        xobjCommand.Parameters.AddWithValue("@nim", nim)
        xobjCommand.Parameters.AddWithValue("@nama", nama)

        Me.xobjConnection.Open()
        Try
            xobjCommand.ExecuteNonQuery()
        Catch mySqlExceptionErr As MySqlException
            MessageBox.Show(mySqlExceptionErr.Message)
        End Try
        Me.xobjConnection.Close()

    End Sub
End Class
--------------------------------

the message of this error is "column nim cannot be null"

//  i use 2 input box, and 1 button.
//  use phptriad. 
//  database = test1
// database = apple -> table = apple
// database = apple -> table = apple -> field = nim (varchar) primary key
                                                      -> field = nama (varchar) not primary key

// i used the newst mySQL_ connector  
// used visualstudio2005

Please help me.
sorry, and thanks (n_n)

Try using the following:

If nim = vbnullstring Then
                xobjCommand.Parameters.AddWithValue("@nim", dBNull.Value)
            Else
                xobjCommand.Parameters.AddWithValue("@nim", nim)
            End If

hemm. it dosen't work..
the message error still same "column nim cannot be null"

...
i have try to check the 'nim' and 'nama'
//using
msgbox (nim) // and
msgbox (nama)

and put that syntax above
Me.xobjConnection.Open()

and the output of msgbox is same with what i have write on 'textbox'
so nim and nama wasn't null

The problem seems to be that the values are not being set. Try this alternate method its basically doing the same thing:

xobjCommand.Parameters.Add("@nim",varchar)
xobjCommand.Parameters.Add("@nama",varchar)

xobjCommand.Parameters("@nim").value=nim
xobjCommand.Parameters("@nama").value=nama

when i use that
there are message error
'Parameter '@nim' not found in the collection'

This article has been dead for over six months. Start a new discussion instead.