2
votes

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?

5
availableID is numeric field ? - matzone
yes. availableID is numeric field. both access and c# - James Kevin De Jesus
If avaialable ID is numeric field then why is it passed in single quotes..Try removing the quotes something like Where AvailableID="+oldAvailable.AvailableID+" - Rohit
Thanks, Kyle. The code is working now and the record can be finally updated. - James Kevin De Jesus

5 Answers

10
votes

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.

1
votes

remove (' ') for those has a integer value

0
votes

Try this

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();
0
votes

simple is to put a debug point on this and check the query. Copy it and run it in access directly and make changes with data. You will find out what parameter value you are entering is wrong.

0
votes

I would use something like this to achieve what you are trying to do. This code works fine for me with MS Access 2013

//setting up connection.
OleDbConnection conn = new OledbConnection("connectionstring goes here");
//set the command string query
string cmdStr = "UPDATE Available SET ProductType = ?, Brand = ?, Model = ?, SerialNo = ?, Remarks = ?, RAM = ?, ODD = ?, VideoCard = ?, PS = ?  WHERE AvailableID = ?";
//create the command 
OleDbCommand cmd = new OleDbCommand(cmdStr,conn);
//add parameters
cmd.Parameters.AddWithValue("@p1",newAvailable.Brand);
cmd.Parameters.AddWithValue("@p2",newAvailable.Brand);
cmd.Parameters.AddWithValue("@p3",newAvailable.SerialNo);
// add all your parameters in the correct order here below .