Question posted 2013 · +2 upvotes
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?
Accepted answer +10 upvotes
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.
Top ms-access Q&A (6)
- How can I modify a saved Microsoft Access 2007 or 2010 Import Specification? +31 (2008)
- OleDbCommand parameters order and priority +28 (2009)
- Is there an equivalent to the SUBSTRING function in MS Access SQL? +26 (2009)
- What do I need to read Microsoft Access databases using Python? +25 (2009)
- MS Access library for python +24 (2009)
- is there any replacement of Access? +21 (2009)
ms-access solutions on this site
.