insertion in vb.net ( it returns syntax error in insert to statement in a textbox)

54 views Asked by At
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim con As New OleDbConnection("provider= microsoft.jet.oledb.4.0;data source=" & CurDir() & "\bilingual1.mdb")
        Dim reader As OleDbDataReader
        Dim cmd As New OleDbCommand
        Try
            con.Open()
            Dim str As String
            str = " insert to yoruba (ọro,itumo,geesi) values ('" & TextBox1.Text & "', '" & RichTextBox1.Text & "', '" & RichTextBox2.Text & "')"
            cmd = New OleDbCommand(str, con)
            reader = cmd.ExecuteReader
            MsgBox("new word added.")
            con.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Dispose()
        End Try
1

There are 1 answers

0
tbm0115 On

You shouldn't need a Reader for an INSERT command. I changed the Cmd method to ExecuteNonQuery() for INSERT command and reformatted the command text for readability.



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim con As New OleDbConnection("provider= microsoft.jet.oledb.4.0;data source=" & CurDir() & "\bilingual1.mdb")
    Dim cmd As OleDbCommand
    Try
        con.Open()
        Dim str As String
        str = "INSERT INTO [yoruba] (ọro,itumo,geesi) VALUES ('" & TextBox1.Text & "', '" & RichTextBox1.Text & "', '" & RichTextBox2.Text & "');"
        cmd = New OleDbCommand(str, con)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("new word added.")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        con.Dispose()
    End Try