Member 13151446 Ответов: 1

Где использовать try и catch для обработки исключения в следующем коде?


Я хочу обрабатывать use try и catch в коде, но я не знаю, где использовать в моем следующем коде

Что я уже пробовал:

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();
               }
           }
       }

1 Ответов

Рейтинг:
1

RickZeeland

Все очень просто, если вы не знаете, куда все положить. try ... catch :)
Когда позже вы обнаружите, что вам нужно более конкретное обнаружение ошибок, вы всегда можете добавить больше.
Пример:

try
    {
       // your code ...
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception Message: " + ex.Message);
    }