Marcus Dutton

Marcus Dutton

  • NA
  • 29
  • 5.6k

Button Click event with if statement.

Dec 18 2015 6:47 PM
Is there a way to run a method in a button click event.  Say an update method, that if the method updates successfully it returns a value of true and if not then false?
 
Basically I have a class that houses my connection string, db insert, update, and delete methods, what I need to do is on my form that initiates the update method I need it to update a statusbar text field with a timer. 
 
here is the code from my form with the button.
      private void btnUpdate_Click(object sender, EventArgs e)
{
Employee upEmp = new Employee();
upEmp.FullName1 = tbxFullName.Text;
upEmp.ShortName1 = tbxShortName.Text;
upEmp.Title1 = cbxTitle.Text;
upEmp.OfficePhone1 = cbxOfficePhone.Text;
upEmp.HomePhone1 = mtbHomePhone.Text;
upEmp.MobilePhone1 = mtbMobilePhone.Text;
upEmp.DateOfBirth1 = Convert.ToDateTime(mtbxDoB.Text);
upEmp.Active1 = cbxActive.Text;
upEmp.Type1 = cbxType.Text;
upEmp.EmergPhone1 = mtbContactPhone.Text;
upEmp.EmergContact1 = tbxEmergContact.Text;
int index = empMainListView.SelectedItems[0].Index;
List<Employee> empUpdate = dbconnect.Load();
upEmp.ID1 = empUpdate[index].ID1;
dbconnect.Update(upEmp);
}
 here is the code my my class file that houses my connection string and update method
 
 
public void Update(Employee upEmp)
{
try
{
dbcommand.CommandText = "UPDATE tblEmployee SET FullName=@vFullName, ShortName=@vShortName, Title=@vTitle, OfficePhone=@vOffice, HomePhone=@vHome, MobilePHone=@vMobile, DateOfBirth=@vDob, Type=@vType, Active=@vActive, EmergPhone=@vEPhone, EmergContact=@vEContact WHERE ID=@vID";
dbcommand.Parameters.AddWithValue("@vFullName", upEmp.FullName1);
dbcommand.Parameters.AddWithValue("@vShortName", upEmp.ShortName1);
dbcommand.Parameters.AddWithValue("@vTitle", upEmp.Title1);
dbcommand.Parameters.AddWithValue("@vOffice", upEmp.OfficePhone1);
dbcommand.Parameters.AddWithValue("@vHome", upEmp.HomePhone1);
dbcommand.Parameters.AddWithValue("@vMobile", upEmp.MobilePhone1);
dbcommand.Parameters.AddWithValue("@vDob", upEmp.DateOfBirth1);
dbcommand.Parameters.AddWithValue("@vType", upEmp.Type1);
dbcommand.Parameters.AddWithValue("@vActive", upEmp.Active1);
dbcommand.Parameters.AddWithValue("@vEPhone", upEmp.EmergPhone1);
dbcommand.Parameters.AddWithValue("@vEContact", upEmp.EmergContact1);
dbcommand.Parameters.AddWithValue("@vID", upEmp.ID1);
dbcommand.CommandType = System.Data.CommandType.Text;
dbconnect.Open();
dbcommand.ExecuteNonQuery();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
dbconnect.Close();
}
}

Answers (7)