How can I get this query to work properly? I can not save any of the data to the database, keep getting error: "Syntax error in INSERT INTO statement"
Data Types was my issue, thank you guys!
How can I get this query to work properly? I can not save any of the data to the database, keep getting error: "Syntax error in INSERT INTO statement"
Data Types was my issue, thank you guys!
The VALUES
clause should be a list of items enclosed in parentheses, ()
. Yours omitted the closing )
.
Your real problem is to forget to use )
at the end of VALUES
part.
INSERT INTO SetUp_Track
values(@Operat, @Maching, @Last_Good_Time,
@First_Good_Time, @Post_Number, @Created
^here
Always try your command in your database manager first. But more important, OleDbCommand
does not accept named parameters.
From OleDbCommand.Parameters
property
The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder
mustshould be used. For example:SELECT * FROM Customers WHERE CustomerID = ?
Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.
Also use using
statement to dispose your OleDbConnection
and OleDbCommand
.
Try not using named parameter:
ad.InsertCommand = new OleDbCommand("INSERT INTO SetUp_Track (Operat, Maching, Last_Good_Time, First_Good_Time, Post_Number, Created) values (?, ?, ?, ?, ?, ?)", con);
Also,
All parameters in .Parameters.AddWithValue are sent with type of String,
check if all of these parameters are defined as String type in db too,
For example if Post_Number is Integer in db then you should cast the String to Integer and then use .Parameters.AddWithValue
ad.InsertCommand.Parameters.AddWithValue("@Post_Number", Convert.ToInt32(Post_NumberTxtbx.Text));