Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I created a database in ms access, in the database I have a table with two columns, fist column is Name with Text data type, and the other column is Date with Date/time data type. and I want to insert values in the table, but it give this error for the month calendar

Data type mismatch in criteria expression.


C#
string sqlQuery = "INSERT INTO register (`Name`,`Date`) values (?,?)";
                using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\DB.accdb"))
                using (OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
                {

                    conn.Open();
                    cmd.Parameters.AddWithValue("@Name", this.textBox1.Text);
                    cmd.Parameters.AddWithValue("@Date", this.monthCalendar1.Text);
                    cmd.ExecuteNonQuery();
                    }
Posted
Comments
Herman<T>.Instance 6-Nov-15 3:29am    
doesn't have the monthCalender1 a SelectedDate as property to use?

Try getting the Calendar.SelectedDate[^] property.

Something like-
C#
cmd.Parameters.AddWithValue("@Date", this.monthCalendar1.SelectedValue);


Hope, it helps :)
 
Share this answer
 
Comments
R.M49 6-Nov-15 4:03am    
It doesn't work 'System.Windows.Forms.MonthCalendar' does not contain a definition for 'SelectedValue' and no extension method 'SelectedValue' accepting a first argument of type 'System.Windows.Forms.MonthCalendar' could be found (are you missing a using directive or an assembly reference?)
Hi, Date is the reserved keyword in MS Access. So change you query like the following,

INSERT INTO register (Name,[Date]) values (?,?)
 
Share this answer
 
Comments
R.M49 6-Nov-15 4:02am    
I changed and the error remained

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