Question

I am loading data into textboxes from an access database to be edited and updated. When I save the data I get Data Type Mismatch error...

I am wanting to update the OCR Title like coded below but only on the selected row which I have tried to do by identifying the OCR number in the text box above OCR Title so then I can only update that specific row and not all of them.

string strSql = "UPDATE Responses SET [OCR Title] = '" + textBox6.Text + "' where OCR = '" + textBox5.Text + "'";

using (OleDbConnection newConn = new OleDbConnection(strProvider))
{
     using (OleDbCommand dbCmd = new OleDbCommand(strSql, newConn))
     {
         dbCmd.CommandType = CommandType.Text;
         dbCmd.Parameters.AddWithValue("OCR", textBox5.Text);
         dbCmd.Parameters.AddWithValue("DeadlineDate", textBox7.Text);
         dbCmd.Parameters.AddWithValue("[OCR Title]", textBox6.Text);
         newConn.Open();
         dbCmd.ExecuteNonQuery();
     }
}
Was it helpful?

Solution

Problem : you have not implemented parameterised queries properly.

Solution : you need to provide values to the parameters not to the column names.

Try This:

string strSql = "UPDATE Responses SET [OCR Title] = @OCRTitle where OCR = @OCR";

    using (OleDbConnection newConn = new OleDbConnection(strProvider))
    {
        using (OleDbCommand dbCmd = new OleDbCommand(strSql, newConn))
        {
            dbCmd.CommandType = CommandType.Text;
            dbCmd.Parameters.AddWithValue("@OCRTitle", textBox6.Text);
            dbCmd.Parameters.AddWithValue("@OCR", textBox5.Text);
            newConn.Open();
            dbCmd.ExecuteNonQuery();
        }
    }

OTHER TIPS

You can't parameterize columns. You can parameterize values.

I don't see any parameter in your dbCmd. And sounds like you don't need to add textBox7.Text as a parameter.

string strSql = "UPDATE Responses SET [OCR Title] = ?
                 where OCR = ?"; 
using (OleDbConnection newConn = new OleDbConnection(strProvider))
{
     using (OleDbCommand dbCmd = new OleDbCommand(strSql, newConn))
     {
          dbCmd.CommandType = CommandType.Text;
          dbCmd.Parameters.AddWithValue("@OCRTitle", textBox6.Text);
          dbCmd.Parameters.AddWithValue("@OCR", textBox5.Text);
          newConn.Open();
          dbCmd.ExecuteNonQuery();
     }
}

Be aware, in OleDb, parameters order are important.

From OleDbCommand.Parameters

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

Your update statement doesn't require a value for the field DeadLineDate, so you should remove it from the parameters collection.
Of course, using a parameterized query, require to mark the position of the parameters with the ? placeholder

    string strSql = "UPDATE Responses SET [OCR Title] = ? where OCR = ?";
    using (OleDbConnection newConn = new OleDbConnection(strProvider))
    {
        using (OleDbCommand dbCmd = new OleDbCommand(strSql, newConn))
        {
            dbCmd.CommandType = CommandType.Text;
            dbCmd.Parameters.AddWithValue("@p1", textBox5.Text);
            dbCmd.Parameters.AddWithValue("@p2", textBox6.Text);
            newConn.Open();
            dbCmd.ExecuteNonQuery();
        }
    }

Instead, if you have to update also the field for the DeadlineDate, you need to convert the value to a date time because this is the only way to properly pass a date using a parameter

string strSql = "UPDATE Responses SET [OCR Title] = ?, DeadlineDate = ? " + 
                "where OCR = ?";

using (OleDbConnection newConn = new OleDbConnection(strProvider))
{
     using (OleDbCommand dbCmd = new OleDbCommand(strSql, newConn))
     {
         dbCmd.CommandType = CommandType.Text;
         dbCmd.Parameters.AddWithValue("@p1", textBox5.Text);
         dbCmd.Parameters.AddWithValue("@p2", Convert.ToDateTime(textBox7.Text));
         dbCmd.Parameters.AddWithValue("@p3", textBox6.Text);
         newConn.Open();
         dbCmd.ExecuteNonQuery();
     }
}

Also notice how the OleDb doesn't recognize the parameters by their names, it inserts the values of the parameters according to the progressive position of the placeholder, so you could name your parameters in any way you like but you should respect the order in which they are needed in the query

If the datatype for DeadlineDate is of type DateTime in your database you will have to convert it the textbox value to DateTime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top