4

access 2003 vs 2010 c#

I cannot see where I have gone wrong. There is no error but no data is being updated. I have the insert, delete and edit working but I don't know why I can't get this to work. Please can someone kindly help me here, thanks in advance...

connection string

 myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
 Source=C:..\TempDB.mdb");

Update method...

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "UPDATE [Family] SET [FirstName] = ?, [LastName] = ?, [FamilyDOB] = ?, [Medical] = ? WHERE [ID] = ?";
        //tried this as well
        //cmd.CommandText = "UPDATE [Family] SET [FirstName] = FirstName, [LastName] = @LastName, [DOB] = @StudentDOB, [Medical] = @Medical WHERE [ID] = @ID";
        cmd.Parameters.AddWithValue("@ID", txtFamID.Text);
        cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@FamDOB", txtFamDOB.Text);
        cmd.Parameters.AddWithValue("@Medical", txtMedical.Text);

        cmd.Connection = myCon;
        myCon.Open();
        cmd.ExecuteNonQuery();
        myCon.Close();
    }
0

2 Answers 2

7

Supply the parameter values in the same order as they appear in the SQL statement.

cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@FamDOB", txtFamDOB.Text);
cmd.Parameters.AddWithValue("@Medical", txtMedical.Text);
cmd.Parameters.AddWithValue("@ID", txtFamID.Text);

OleDB plus MS Access doesn't care about the parameter names, only their order.

1
  • Thanks for that. I wasn't aware that sql statement for update required to be in same order, Most appreciated. Commented Mar 11, 2013 at 19:24
3

The OLE DB.NET Framework Data Provider uses positional parameters that are marked with a question mark (?) instead of named parameters.

Change this:

    cmd.Parameters.AddWithValue("@ID", txtFamID.Text);
    cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
    cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
    cmd.Parameters.AddWithValue("@FamDOB", txtFamDOB.Text);
    cmd.Parameters.AddWithValue("@Medical", txtMedical.Text);

to:

    cmd.Parameters.AddWithValue("?", txtFamID.Text);
    cmd.Parameters.AddWithValue("?", txtFirstName.Text);
    cmd.Parameters.AddWithValue("?", txtLastName.Text);
    cmd.Parameters.AddWithValue("?", txtFamDOB.Text);
    cmd.Parameters.AddWithValue("?", txtMedical.Text);

More: OleDbParameter Class

6
  • Completely forgot that, @Iswanto Sam. I have voted to delete my answer, and +1 to yours.
    – Brian
    Commented Mar 11, 2013 at 18:31
  • @Iswanto - can you claify what should be done at cmd.CommandText = "UPDATE....thanks Commented Mar 11, 2013 at 18:34
  • @bucketblast: just use your current code, use ? for the parameter Commented Mar 11, 2013 at 18:36
  • @Iswanto - either of them made any difference? Commented Mar 11, 2013 at 18:38
  • @bucketblast: Are you sure the ID that you use for UPDATE is exists in the table? Commented Mar 11, 2013 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.