I want to handle use try and catch in code but I don't know where to use in my following code
What I have tried:
private void btnPUpdate_Click(object sender, EventArgs e)
{
if ((txtPVNumber.Text == "") || (cboPVColor.Text == "") || (cboPVType.Text == "") || (cboPVBrand.Text == "") || (txtPDaysLeft.Text == "") || (txtPOName.Text == "") || (txtPCivilID.Text == "") || (txtPTelephone.Text == ""))
{
MessageBox.Show("Please select a recored to Update");
}
else
{
DialogResult upd = MessageBox.Show("Are you Sure you want to Update?" + txtPVNumber.Text + "", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (upd == DialogResult.Yes)
{
using (var connection = new SqlConnection(@"Data Source=DESKTOP-F1TCIFJ;Initial Catalog=tempdb;Integrated Security=True"))
{
using (var command = new SqlCommand("UPDATE personal SET VNumber, VColor, VType, VBrand, ExpiryDate, DaysLeft, OName, CivilID, Telephone ) VALUES (@vnumber, @vcolor, @vtype, @vbrand, @expirydate, @daysleft, @ownername, @civilid, @telephone )", connection))
{
command.Parameters.AddWithValue("@vcolor", cboPVColor.Text);
command.Parameters.AddWithValue("@vtype", cboPVType.Text);
command.Parameters.AddWithValue("@vbrand", cboPVBrand.Text);
command.Parameters.AddWithValue("@expirydate", dateTimePickerPersonal.Value.ToString("MM/dd/yyyy"));
command.Parameters.AddWithValue("@daysleft", txtPDaysLeft.Text);
command.Parameters.AddWithValue("@ownername", txtPOName.Text);
command.Parameters.AddWithValue("@civilid", txtPCivilID.Text);
command.Parameters.AddWithValue("@telephone", txtPTelephone.Text);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
txtPVNumber.Text = "";
cboPVColor.Text = "";
cboPVType.Text = "";
cboPVBrand.Text = "";
dateTimePickerPersonal.Value = DateTime.Now;
txtPDaysLeft.Text = "";
txtPOName.Text = "";
txtPCivilID.Text = "";
txtPTelephone.Text = "";
btnPSave.Enabled = true;
}
}
}
else
{
txtPVNumber.Text = "";
cboPVColor.Text = "";
cboPVType.Text = "";
cboPVBrand.Text = "";
dateTimePickerPersonal.Value = DateTime.Now;
txtPDaysLeft.Text = "";
txtPOName.Text = "";
txtPCivilID.Text = "";
txtPTelephone.Text = "";
btnPSave.Enabled = true;
this.Show();
}
}
}