Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i had tried to insert the values to the database by using insert into statement , the bulid process is succeeded , but at the runtime i am getting the error, that ("Syntax error in INSERT INTO statement.") ,



VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Customer.mdb")
        con.Open()
        Dim s1 As String = "insert into dbadd(input,output,project)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "') "
        cmd = New OleDbCommand(s1, con)
        cmd.ExecuteNonQuery()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        MsgBox("Successfully inserted")
    End Sub
Posted

There is Syntax error in your query.There are so many ways to write query to insert data into database.Try these one :
VB
Dim objda As New OleDbDataAdapter
obj.SelectCommnd=New OledbCommand
objda.SelectCommand.CommandText="Insert into TableName (Field1, Field2, Field3) values(@Field1, @Field2, @Field3)"
objda.SelectCommand.Parameters.AddWithValue("@Field1", TextBox1.Text)
objda.SelectCommand.Parameters.AddWithValue("@Field2", TextBox2.Text)
objda.SelectCommand.Parameters.AddWithValue("@Field3", TextBox3.Text)
   Try                                     'To handle OleDb Exceptions
       objda.SelectCommand.ExecuteNonQuery()
   Catch ex As OleDb.OleDbException
       MessageBox.Show(ex.Message)
   End Try

I hope it will help you :)
 
Share this answer
 
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Customer.mdb")
        con.Open()
TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        Dim s1 As String = "insert into dbadd(input,output,project)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "') "
        cmd = New OleDbCommand(s1, con)
        cmd.ExecuteNonQuery()

        MsgBox("Successfully inserted")
    End Sub




try and say whether it works now
 
Share this answer
 
v3
Comments
[no name] 29-Nov-11 0:40am    
initializing the values of the textboxes to empty would insert empty string values to database
ANANTH P 29-Nov-11 0:41am    
no sir same problem exists

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