本文介绍了在下面的代码中使用try和catch来处理异常的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理使用try和catch代码但我不知道在以下代码中使用的地方



我尝试过:



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

推荐答案

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


这篇关于在下面的代码中使用try和catch来处理异常的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:43