Question

I have a database which has a table named Schedule. It looks like this:

Schedule
--------
ScheduleID (number)
BackupID   (number)
Repository (text)
Interval   (text)
Type       (text)

Now I am trying to insert values into this table like this:

List<string> scheduleData = this.scheduler.getSchedule();
string repository = scheduleData[0];
string interval = scheduleData[1];
string type = scheduleData[2];

List<string> selectedBackup = this.backupForm.getSelectedUser();
string backupID = selectedBackup[0];

string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\db\\dbBackupUtility.mdb";
OleDbConnection conn = new OleDbConnection(connetionString);
conn.Open();
OleDbCommand sql = new OleDbCommand("INSERT INTO Schedule (BackupID, Repository, Interval, Type) VALUES (@BackupID, @Repository, @Interval, @Type)", conn);
sql.Parameters.AddWithValue("@BackupID", backupID);
sql.Parameters.AddWithValue("@Repository", repository);
sql.Parameters.AddWithValue("@Interval", interval);
sql.Parameters.AddWithValue("@Type", type);

sql.ExecuteNonQuery();

conn.Close();  

Howevery, I get a OleDbException everytime the program executes this line:

sql.ExecuteNonQuery();

I can't see any other information about the exception but I have tried inserting the values manually using the query analyzer in VS, which strangely works. I have already tried the above with a reader or adapter but I get the same error every time...

Exception Text:

Message: "Syntax error in INSERT statement"
StackTrace =    "at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at WindowsFormsApplication1.BackupUtility.scheduler_VisibleChanged(Object sender, EventArgs e) in c:\Users\Teichler\Dropbox\VS_Projects\Projects\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs:Line104."
Inner Exception: null
Was it helpful?

Solution

Interval is a keyword for Jet/Access, change your command into this (surround Interval with brackets):

OleDbCommand sql = new OleDbCommand("INSERT INTO Schedule (BackupID, Repository, [Interval], Type) VALUES (@BackupID, @Repository, @Interval, @Type)", conn);

OTHER TIPS

This error is comming because you have one value null which you are passing as a parameter to insert. Your table column for that value does not accepts NULL.

Debugg your code. Identify NULL variable and make it in proper datatype.

Use ? instaed @.

Its maybe because Type is a keyword in sql. Try changing your statement from

"INSERT INTO Schedule (BackupID, Repository, Interval, Type)

to

"INSERT INTO Schedule (BackupID, Repository, Interval, [Type])

surrounding Type in brackets

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top