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();
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With