I have an access file with 7 fields:
DocID - text - primary SourceID - text ReceivedDay - Date/Time Summary - text DueDay - Date/Time Person - text Status - Yes/No
Now I want to update this file with the following code:
const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\DocMan.mdb;Persist Security Info=True"; const string InsertQuery = "INSERT Into Docs(DocID,ReceivedDay,Summary,Person,DueDay,Status,SourceID) Values(@DocID,@ReceivedDay,@Summary,@Person,@DueDay,@Status,@SourceID)"; string DocID = textBox1.Text; string SourceID = comboBox1.SelectedIndex.ToString(); DateTime ReceivedDay = dateTimePicker1.Value; string Summary = richTextBox1.Text; string Person = textBox2.Text; DateTime DueDay = dateTimePicker2.Value; bool Status = false; OleDbConnection cnn = new OleDbConnection(ConnectionString); cnn.Open(); OleDbCommand cmd = new OleDbCommand(InsertQuery, cnn); cmd.Parameters.AddWithValue("@DocID", DocID); cmd.Parameters.AddWithValue("@SourceID", SourceID); cmd.Parameters.AddWithValue("@ReceivedDay", ReceivedDay); cmd.Parameters.AddWithValue("@Summary", Summary); cmd.Parameters.AddWithValue("@Person", Person); cmd.Parameters.AddWithValue("@DueDay", DueDay); cmd.Parameters.AddWithValue("@Status", Status); cmd.ExecuteNonQuery(); cnn.Close();
But I get an exception:
Data type mismatch in criteria expression.
How can i fix this?
EDIT: I fixed this using a different approach:
I built a query like this:
INSERT INTO Docs (DocID, SourceID, ReceivedDay, Summary, Person, DueDay, Status) VALUES (?, ?, ?, ?, ?, ?, ?)
and then use the TableAdapter to call it:
string DocID = textBox1.Text; string SourceID = comboBox1.SelectedIndex.ToString(); DateTime ReceivedDay = dateTimePicker1.Value.Date; string Summary = richTextBox1.Text; string Person = textBox2.Text; DateTime DueDay = dateTimePicker2.Value.Date; bool Status = false; DocManDataSetTableAdapters.DocsTableAdapter docsTableAdapter = new DocManDataSetTableAdapters.DocsTableAdapter(); docsTableAdapter.InsertQuery(DocID,SourceID,ReceivedDay,Summary,Person,DueDay,false);
Much easier and now it works great. Thanks to everyone.