Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

i recently encountered very frustrating error that makes me frustrated. i think my codes are correct specially my sql statement but it gives me an error:
"Syntax error in INSERT INTO statement." Please if there is anyone one of you who knows where did i go wrong kindly correct me and point it out.thanks

My VB.Net 2008 Code using Access 2007 and Windows 7 64 Bit

VB
   Public cn As OleDbConnection
    Public cmd As New OleDbCommand
    Public dr As OleDbDataReader

Public Sub open_connection1()
        ' Try
        cn = New OleDbConnection
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\etks.mdb;Persist Security Info=True;Jet OLEDB:Database Password=@tekibears07"
        cn.Open()
        cmd.Connection = cn
        ' Catch ex As Exception
        '   MessageBox.Show("An Error occured while trying to connect in a loacal database" + vbCrLf + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        ' End Try
    End Sub

Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
        'Try
        If Not txt_emp_name.Text = "" Then
            open_connection1()
            cmd.CommandText = "INSERT INTO attendance (emp_id, emp_name, department, position, dates) VALUES ('" & txt_emp_id.Text & "', '" & txt_emp_name.Text & "', '" & txt_department.Text & "', '" & txt_position.Text & "', '" & txt_attendance_date.Text & "')"
            cmd.ExecuteNonQuery()
            dr.Close() : cn.Close()
            MessageBox.Show("Saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Close()
        Else
            MessageBox.Show("Enter a valid id number.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        'Catch ex As Exception
        'MessageBox.Show("An error occured while entering new attendance" + vbCrLf + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ' End Try

    End Sub
Posted

1 solution

hi,

you are passing each value as a string check your columns data type. other wise there is no problem in SQL sintex
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900