Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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();
               }
           }
       }
Posted
Updated 5-Jul-17 8:51am

1 solution

It's simple, if you don't know where put everything in a try ... catch :)
When you find later that you need more specific error catching you can always add more.
Example:
C#
try
    {
       // your code ...
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception Message: " + ex.Message);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900