I have two form's Search Codes for these Search Codes i want to add try catch exception

  • 88 Views
  • Last Post 04 August 2019
[email protected] posted this 03 August 2019

I have two form's Search codes i need to add try catch exception for them.

first form is including Tank Details there i select data by tank no, date, start time, end time and time duration. for this i want add if tank no and and time duration is selected only it should allow for Search option if not it should show error message.

Second form is including Alarm Details there i select alarm type, date, start time and end time. for this i want add if alarm type is selected only it should allow for Search option if not it should show error message.

Sorry, I don't know how to try, Please help me.

Alarm Details Search part
private void BtnSearch_Click_1(object sender, EventArgs e)
        {
            linkLabel1.Enabled = false;
            Search.Enabled = false;
            btnSearch.Enabled = false;
            btnPrint.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            var startDate = dateTimePicker1.Value.Date.Add(new TimeSpan(dateTimePicker2.Value.Hour, dateTimePicker2.Value.Minute, dateTimePicker2.Value.Second));
            var endDate = dateTimePicker1.Value.Date.Add(new TimeSpan(dateTimePicker3.Value.Hour, dateTimePicker3.Value.Minute, dateTimePicker3.Value.Second));
            DateTime dct;
            DataTable tab = new DataTable();
            tab.Columns.Add(new DataColumn("Active Time"));
            tab.Columns.Add(new DataColumn("Type"));
            if (comboBox1.SelectedIndex == -1)
            {
                tab.Columns.Add(new DataColumn("Name "/* + (comboBox1.SelectedIndex + 1)*/));
            }
            else
            {
                tab.Columns.Add(new DataColumn("Name " /*+ (comboBox1.SelectedIndex)*/));
            }
           
            tab.Columns.Add(new DataColumn("Value/Contents"));
            foreach (DataRow dtr in dt.Rows)
            {
                if (!DateTime.TryParse(dtr.ItemArray[0] as string, out dct))
                    continue;
                DateTime tim;
                DateTime.TryParse(dtr.ItemArray[1] as string, out tim);
                dct = dct.Add(new TimeSpan(tim.Hour, tim.Minute, tim.Second));
                if ((dct >= startDate && dct <= endDate) && (comboBox1.SelectedIndex > -1))
                {
                    if(comboBox1.Text == dtr.ItemArray[1].ToString())
                    {
                        tab.Rows.Add(dtr.ItemArray[0], dtr.ItemArray[1], dtr.ItemArray[2], dtr.ItemArray[3]);
                    }
                 
                }
            }

            dt = tab;
            dataGridView1.DataSource = tab;
            linkLabel1.Enabled = true;
            Search.Enabled = true;
            btnSearch.Enabled = true;
            btnPrint.Enabled = true;
            Cursor.Current = Cursors.Default;
        }

Tank Details Search part
       private async void BtnSearch_Click(object sender, EventArgs e)
        {             
            linkLabel1.Enabled = false;
            Search.Enabled = false;
            btnSearch.Enabled = false;
            btnPrint.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            var tgs=await SearchCSV();
            foreach(var gts in tgs)
            bs.Add(gts);
               
            linkLabel1.Enabled = true;`enter code here`
            Search.Enabled = true;
            btnSearch.Enabled = true;
            btnPrint.Enabled = true;
            Cursor.Current = Cursors.Default;         
        }

Order By: Standard | Newest | Votes
lucy posted this 04 August 2019

If you want to add try catch to event handler you can write as the following c# code.

private void BtnSearch_Click_1(object sender, EventArgs e)
{
   try{
      //put your code here
   }
   catch(Exception ex)
   {
      MessageBox.Show(ex.Message,"Message");
   }
}

Similarly for all event handler

[email protected] posted this 04 August 2019

For the Tank Details Search part is it possible to use the same as above or should i change anything different

please reply asap mam

 

Close