Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# AND ACCESS - Data type mismatch in criteria expression

I've created a code that updates/edits details of a/an computer/electronic product for a C# program connecting to the MS Access. Here are the codes:

OleDbCommand cmd = new OleDbCommand("UPDATE Available SET ProductType = '" + newAvailable.ProductType + "', Brand = '"+ newAvailable.Brand + "', Model = '" + newAvailable.Model + "', SerialNo = '" + newAvailable.SerialNo + "', Remarks = '" + newAvailable.Remarks + "', RAM = '" + newAvailable.RAM + "', HDD = '" + newAvailable.HDD + "', ODD = '" + newAvailable.ODD + "', VideoCard = '" + newAvailable.VideoCard + "', PS = '" + newAvailable.PS + "'  WHERE AvailableID = '"+oldAvailable.AvailableID+"'", cnn);
cmd.CommandType = CommandType.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();

AvailableID accepts Int32 values and the rest of the variables are string. The program is executable, yet the C# detected the error.

Data type mismatch in criteria expression.

What should I do?

like image 591
James Kevin De Jesus Avatar asked Dec 17 '25 18:12

James Kevin De Jesus


1 Answers

I suspect that you're not passing one of your parameters correct probably the AvailableID, instead try to add the parameters this way:

var cmd = new OleDbCommand
{
    Connection = cnn,
    CommandType = CommandType.Text,
    CommandText = "UPDATE Available SET ProductType = ?, Brand = ?, Model = ?, SerialNo = ?, Remarks = ?, RAM = ?, ODD = ?, VideoCard = ?, PS = ?  WHERE AvailableID = ?"
};

cmd.Parameters.Add(new OleDbParameter {Value = newAvailable.ProductType});
cmd.Parameters.Add(new OleDbParameter {Value = newAvailable.Brand});
// add the other parameters ...

As a side note, it's not a good idea to generate queries by concatenating strings anyway you should always use parameters.

like image 50
Dimitar Dimitrov Avatar answered Dec 20 '25 10:12

Dimitar Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!