c# - How do I Inform user that an element does not exist in a table? -


i have details form, , know using try , catch way of validation here bad practice. how check see if custid exists , tell user entered not exist?

apologies if silly question , it's obvious and..., i'm beginner.

public void getdetails() {     lblmessage.text = "";     if (txtcid.text == "")     {         lblmessage.text = "please enter customer id before obtaining details.";     }     else     {         command.connection.open();         command.connection = conn;                         command.commandtype = commandtype.storedprocedure;         command.commandtext = "getcustomer";         sqlparameter param = new sqlparameter();         param.parametername = "@custid";         param.sqldbtype = sqldbtype.int;         param.direction = parameterdirection.input;         param.value = txtcid.text;          command.parameters.add(param);         adapter.selectcommand = command;         adapter.fill(table);          txtfname.text = table.rows[0].field<string>("firstname");         txtfname.databind();         txtlname.text = table.rows[0].field<string>("surname");         txtlname.databind();         rdogender.text = table.rows[0].field<string>("gender").tostring();         txtage.databind();         txtage.text = table.rows[0].field<int>("age").tostring();         txtage.databind();         txtadd1.text = table.rows[0].field<string>("address1").tostring();         txtage.databind();         txtadd2.text = table.rows[0].field<string>("address2").tostring();         txtage.databind();         txtcity.text = table.rows[0].field<string>("city").tostring();         txtage.databind();         txtphone.text = table.rows[0].field<string>("phone").tostring();         txtage.databind();         txtmobile.text = table.rows[0].field<string>("mobile").tostring();         txtage.databind();         txtemail.text = table.rows[0].field<string>("email").tostring();         txtemail.databind();          command.connection.close();     } } 

i'm not sure understood question. is:

public bool getdetails() {     bool found = false;     int id;     bool isnumber;     lblmessage.text = "";     isnumber = int.tryparse(txtcid.text, out id);     if (!isnumber)     {         lblmessage.text = "please enter valid customer id before obtaining details.";     }     else     {         command.connection.open();         command.connection = conn;                         command.commandtype = commandtype.storedprocedure;         command.commandtext = "getcustomer";         sqlparameter param = new sqlparameter();         param.parametername = "@custid";         param.sqldbtype = sqldbtype.int;         param.direction = parameterdirection.input;         param.value = id;          command.parameters.add(param);         adapter.selectcommand = command;         adapter.fill(table);          if (table.rows.count > 0)         {           txtfname.text = table.rows[0].field<string>("firstname");           txtfname.databind();           txtlname.text = table.rows[0].field<string>("surname");           txtlname.databind();           rdogender.text = table.rows[0].field<string>("gender").tostring();           txtage.databind();           txtage.text = table.rows[0].field<int>("age").tostring();           txtage.databind();           txtadd1.text = table.rows[0].field<string>("address1").tostring();           txtage.databind();           txtadd2.text = table.rows[0].field<string>("address2").tostring();           txtage.databind();           txtcity.text = table.rows[0].field<string>("city").tostring();           txtage.databind();           txtphone.text = table.rows[0].field<string>("phone").tostring();           txtage.databind();           txtmobile.text = table.rows[0].field<string>("mobile").tostring();           txtage.databind();           txtemail.text = table.rows[0].field<string>("email").tostring();           txtemail.databind();           found = true;         }         else         {            lblmessage.text = "user id " + id + " not exists";         }         command.connection.close();     }     return found; } 

the function return false if either id not specified or not exist. problem don't check if txtcid.text contains valid number: in case sql error thrown! added number conversion check ensures @ least stored procedure execution runs without errors. anyway, should wrap whole procedure in try-catch intercept unpredictable error (db offline or internal db error, etc). then, use table.rows.count verify if stored procedure returned result.

mario.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -