Reputation: 51
I've created a WPF application that is able to interface with an ACCESS DATABASE file. Unfortuntely, when I try to write to it I'm getting a syntax error. If I remove the necessary info for the Usage column on my OleDB command, it works. But something isn't right. I've tried changing the OleDB type to LongVarChar, LongVarWChar, and VarWChar with no luck. The access database file has it setup as a Short Text (see below)
What am I missing? I'm sure its something simple. I've been working on thsi problem for nearly 3 days at this point...
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + IHM_Management_Software.Properties.Settings.Default.Database_Location + "");
OleDbCommand command = new OleDbCommand();
OleDbTransaction transaction = null;
command.Connection = connection;
connection.Open();
try
{
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
command.Connection = connection;
command.Transaction = transaction;
command.CommandText = "Insert INTO UserLog (UserMMSID, TimeIn, TimeOut, Usage) " +
"VALUES (?, ?, ?, ?)";
command.Parameters.Add("?", OleDbType.Double).Value = Double.Parse(GlobalMMSID.ToString());
command.Parameters.Add("?", OleDbType.Date).Value = DateTime.Now;
command.Parameters.Add("?", OleDbType.Date).Value = DBNull.Value;
command.Parameters.Add("?", OleDbType.VarChar, 100).Value = ButtonType.ToString();
Console.WriteLine(command.CommandText);
command.ExecuteNonQuery();
transaction.Commit();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
this.Close();
}
finally
{
//System.Threading.Thread.Sleep(1000);
this.Close();
}
return true;
Upvotes: 0
Views: 515
Reputation: 51
@Steve posted a comment on my question and resolved my problem. Thank you Steve!
The answer was that my fourth column was named "Usage" which is apparently a reserved word and the Error only mentioned syntax error. Adding brackets around the word like so:
[Usage]
Fixed it.
Upvotes: 1