Error/exception handling (try-catch) C# -


how achieve requirements below:

  1. the retrieve button click event:
    • if user doesn't enter custid in txtcustid need inform them enter customer id via lblmessage;
    • if entered custid doesn't exist in database inform user doesn't exist via lblmessage.
  2. the update button click event - need ensure customer id exists in database.
  3. the delete button click event: same requirements retrieve button.

i must use error/exception handling (try-catch) achieve these (project requirement). spent hours trying, no success. grateful help! code below:

namespace acme { public partial class customer : system.web.ui.page {     sqlconnection conn;     sqldataadapter adapter = new sqldataadapter();     datatable table = new datatable();     sqlcommand command = new sqlcommand();      protected void page_load(object sender, eventargs e)     {         conn = new sqlconnection(configurationmanager.         connectionstrings["dbconnection1"].connectionstring);            }      private void page_preinit(object sender, eventargs e)     {         httpcookie settheme = request.cookies.get("userselectedtheme");         if (settheme != null)         {             page.theme = settheme.value;         }     }      protected void clear()     {         txtcustid.text = "";         txtfirstname.text = "";         txtsurname.text = "";         rbtgender.selectedvalue = "";         txtage.text = "";         txtaddress1.text = "";         txtaddress2.text = "";         txtcity.text = "";         txtphone.text = "";         txtmobile.text = "";         txtemail.text = "";         txtemail2.text = "";     }      protected void btnnew_click(object sender, eventargs e)     {         sqldataadapter adapter1 = new sqldataadapter();         datatable table1 = new datatable();         sqlcommand command1 = new sqlcommand();          clear();          conn = new sqlconnection(configurationmanager.         connectionstrings["dbconnection1"].connectionstring);          command1.connection = conn;         command1.commandtype = commandtype.storedprocedure;         command1.commandtext = "largestcustid";         command1.connection.open();          int id = (int)command1.executescalar() + 1;         txtcustid.text = id.tostring();         command1.dispose();         conn.close();      }      protected void btnadd_click(object sender, eventargs e)     {         conn = new sqlconnection(configurationmanager.         connectionstrings["dbconnection1"].connectionstring);          sqlcommand command = new sqlcommand();          command.connection = conn;         command.commandtype = commandtype.storedprocedure;         command.commandtext = "addcustomer";         command.connection.open();          command.parameters.addwithvalue("@custid",                int.parse(txtcustid.text));         command.parameters.addwithvalue("@firstname", txtfirstname.text);         command.parameters.addwithvalue("@surname", txtsurname.text);         command.parameters.addwithvalue("@gender", rbtgender.selectedvalue);         command.parameters.addwithvalue("@age", int.parse(txtage.text));         command.parameters.addwithvalue("@address1", txtaddress1.text);         command.parameters.addwithvalue("@address2", txtaddress2.text);         command.parameters.addwithvalue("@city", txtcity.text);         command.parameters.addwithvalue("@phone", txtphone.text);         command.parameters.addwithvalue("@mobile", txtmobile.text);         command.parameters.addwithvalue("@email", txtemail.text);          adapter.insertcommand = command;         adapter.insertcommand.executenonquery();         lblmessage.text = "the new record has been added database!";          command.connection.close();         clear();      }      protected void btnretrieve_click(object sender, eventargs e)     {         conn = new sqlconnection(configurationmanager.         connectionstrings["dbconnection1"].connectionstring);            sqlcommand command = new sqlcommand();          command.connection = conn;         command.commandtype = commandtype.storedprocedure;         command.commandtext = "getcustid";         command.connection.open();          sqlparameter param = new sqlparameter();         param.parametername = "@custid";         param.sqldbtype = sqldbtype.int;         param.direction = parameterdirection.input;         param.value = int.parse(txtcustid.text);         command.parameters.add(param);          adapter.selectcommand = command;         adapter.fill(table);          int id = table.rows.count;         if (id == 0)         {             lblmessage.text = "customer id not exists!";         }          else         {             lblmessage.text = "";             txtfirstname.text = table.rows[0].field<string>("firstname");             txtfirstname.databind();             txtsurname.text = table.rows[0].field<string>("surname");             txtsurname.databind();             txtage.text = table.rows[0].field<int>("age").tostring();             txtage.databind();             txtaddress1.text = table.rows[0].field<string>("address1");             txtaddress1.databind();             txtaddress2.text = table.rows[0].field<string>("address2");             txtaddress2.databind();             txtcity.text = table.rows[0].field<string>("city");             txtcity.databind();             txtphone.text = table.rows[0].field<string>("phone");             txtphone.databind();             txtmobile.text = table.rows[0].field<string>("mobile");             txtmobile.databind();             txtemail.text = table.rows[0].field<string>("email");             txtemail.databind();         }             command.connection.close();     }      protected void btnupdate_click(object sender, eventargs e)     {         conn = new sqlconnection(configurationmanager.         connectionstrings["dbconnection1"].connectionstring);          sqlcommand command = new sqlcommand();          command.connection = conn;         command.commandtype = commandtype.storedprocedure;         command.commandtext = "updatecustomer";         command.connection.open();          command.parameters.addwithvalue("@custid",            int.parse(txtcustid.text));         command.parameters.addwithvalue("@firstname", txtfirstname.text);         command.parameters.addwithvalue("@surname", txtsurname.text);         command.parameters.addwithvalue("@gender", rbtgender.selectedvalue);         command.parameters.addwithvalue("@age", int.parse(txtage.text));         command.parameters.addwithvalue("@address1", txtaddress1.text);         command.parameters.addwithvalue("@address2", txtaddress2.text);         command.parameters.addwithvalue("@city", txtcity.text);         command.parameters.addwithvalue("@phone", txtphone.text);         command.parameters.addwithvalue("@mobile", txtmobile.text);         command.parameters.addwithvalue("@email", txtemail.text);          lblmessage.text = "the record has been updated!";          command.connection.close();         clear();     }      protected void btndelete_click(object sender, eventargs e)     {          try         {              conn = new sqlconnection(configurationmanager.             connectionstrings["dbconnection1"].connectionstring);              sqlcommand command = new sqlcommand();              command.connection = conn;             command.commandtype = commandtype.storedprocedure;             command.commandtext = "deletecustomer";             command.connection.open();               sqlparameter param = new sqlparameter();             param.parametername = "@custid";             param.sqldbtype = sqldbtype.int;             param.direction = parameterdirection.input;             param.value = int.parse(txtcustid.text);             command.parameters.add(param);              adapter.deletecommand = command;             adapter.deletecommand.executenonquery();              int id = (int)param.value;              command.connection.close();          }         catch (exception ex)         {             lblmessage.text += "please enter customer id!";         }          try         {             lblmessage.text = "";             sqlparameter param = new sqlparameter();             param.parametername = "@custid";             param.sqldbtype = sqldbtype.int;             param.direction = parameterdirection.input;             param.value = int.parse(txtcustid.text);             command.parameters.add(param);              adapter.deletecommand = command;             adapter.deletecommand.executenonquery();              int id = table.rows.count;             id = (int)param.value;              lblmessage.text += "customer record has been deleted";             command.connection.close();          }          catch (exception ex)          {             lblmessage.text = "customer id doesnot exists!";          }            clear();     }        public string custid { get; set; } } } 

it sounds trying control flow of application use of exceptions. there many reasons against approach:

1) code difficult understand , debug.

2) throwing exceptions in .net expensive.

3) if exception control flow of application how differentiate them real exceptions (thrown when doesn't work expected)?

if, on other hand, want throw exception when of scenarios listed in question happens can use standard .net exception class:

if (string.isnullorwhitespace(txtcustid.text)) {     throw new exception("id not provided."); } 

or can create custom exception provide more specific information:

public class idnotprovidedexception : exception {     public string mycommandname { get; set; }      public idnotprovidedexception(string msg)         : base(msg)     {      }      public idnotprovidedexception(string msg, string mycommandname)         : base(msg)     {         this.mycommandname = mycommandname;     } } 

and initialize , throw custom exception.

lastly, there places in code, though not mentioned in question, worth wrapping in try...catch block. basically, place connect server may result in unexpected (for instance, server may not available).


Popular posts from this blog