Reputation: 1
private void button1_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["connection_string"].ConnectionString;
OleDbConnection C = new OleDbConnection(CS);
C.Open();
OleDbCommand CMD = new OleDbCommand("", C);
CMD.CommandText = "SELECT COUNT(*) FROM Applicants WHERE ApplicantID = ?";
CMD.Parameters.AddWithValue("@ApplicantID", textBox1.Text);
if (Convert.ToInt32(CMD.ExecuteScalar()) > 0)
{
CMD.CommandText = "UPDATE Onboarding SET " +
"BackgroudCheck = @BGC, PhotoID = @PID, " +
"TrainingHoursOutOf40 = @THOO, DrugTestTaken = @DTT, PassedDrugTest = @PDT" +
"WHERE ApplicantID = @AID";
CMD.Parameters.AddWithValue("@AID", textBox1.Text);
CMD.Parameters.AddWithValue("@BGC", CheckBox1);
CMD.Parameters.AddWithValue("@PID", CheckBox2);
CMD.Parameters.AddWithValue("@THOO", TextBox2.Text);
CMD.Parameters.AddWithValue("@DTT", CheckBox3);
CMD.Parameters.AddWithValue("@PDT", CheckBox4);
MessageBox.Show("Applicant Updated Successfully");
}
else
{
MessageBox.Show("Applicant Does Not Exists");
}
C.Close();
}
I am trying to update the data table. Some how when I try to update in the form I do get a messagebox saying "Applicant updated Successfully", but when I go to the data table or view it in another form, the table is not updated.
Upvotes: 0
Views: 57
Reputation: 74730
Several problems here
using
to ensure database resources are closed and disposed of when you're done with themc.Execute("UPDATE ...", new { bgcCheckbox.Checked, ... aidTextbox.Text })
private void button1_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["connection_string"].ConnectionString;
using(OleDbConnection c = new OleDbConnection(cs)){
c.Open();
using(OleDbCommand cmd = new OleDbCommand("", c)){
cmd.CommandText = "UPDATE Onboarding SET " +
"BackgroudCheck = ?, PhotoID = ?, " +
"TrainingHoursOutOf40 = ?, DrugTestTaken = ?, PassedDrugTest = ?" +
"WHERE ApplicantID = ?";
cmd.Parameters.AddWithValue("@BGC", CheckBox1.Checked);
cmd.Parameters.AddWithValue("@PID", CheckBox2.Checked);
cmd.Parameters.AddWithValue("@THOO", TextBox2.Text);
cmd.Parameters.AddWithValue("@DTT", CheckBox3.Checked);
cmd.Parameters.AddWithValue("@PDT", CheckBox4.Checked);
cmd.Parameters.AddWithValue("@AID", textBox1.Text);
if(cmd.ExecuteNonQuery()>0)
MessageBox.Show("Applicant Updated Successfully");
else
MessageBox.Show("Applicant Does Not Exists");
}
}
}
Upvotes: 3
Reputation: 81
You have not used ExecuteNonQuery() method of SQL Command class.
int returnValue;
returnValue = CMD.ExecuteNonQuery();
Upvotes: 1