Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, am getting an error that is
Datetyoe mismatch in criteria expression
everything is perfect fine but when i add date columns so i get this error please guys solve it please

What I have tried:

con_string.Open();
                    OleDbCommand command = new OleDbCommand() { Connection = con_string };
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        command.CommandText = @"insert into [Total]([column3],[column2],[Flavours],[Remarks],[Column1],[Extra Topping],[Topping Price],[Date])
                        VALUES (@Column3, @Column2, @Flavours, @Remarks, @Column1, @ExtraTopping, @ToppingPrice,@Date)";
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@Column3", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Column2", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Flavours", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Remarks", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Column1", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@ExtraTopping", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@ToppingPrice", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells[0].Value.ToString());

                        command.ExecuteNonQuery();
                    }
                    con_string.Close()
Posted
Updated 4-Jul-17 5:53am
Comments
F-ES Sitecore 4-Jul-17 11:51am    
One of more bits of data you are passing are the wrong type. We don't know what types your data needs as we don't have the schema and we don't know what types you are giving as we don't know the inputs. You really need to learn to debug your own code so you don't have to start a thread for every little thing.

Off the bat you are passing the same value for everything (dataGridView1.Rows[i].Cells[0]) and it's highly unlikely that is what you actually meant to do, or that one value can possibly be text, a number, a date etc.

1 solution

Don't convert the date to a string to pass it to SQL: if it's a DateTime value already, pass it as such. If not, use TryParse to convert it to a DateTime, and then pass that.

The trouble with string dates is that they aren't the same for all systems: the locale for an SQL Server instance can be different from that your code runs on, and what looks ok to you "23/12/10" looks bad to SQL because it's expecting MM/dd/yyyy and you passed it yy/MM/dd.
I also seriously doubt that the same row and column contains the right value for all of the columns you are trying to INSERT it into - you had noticed that you use the same cell for every parameter?
 
Share this answer
 

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