c# - check for same user name in database 3 -


i want check whether user exist or not , if not save in database else show error message user exist before used command works perfectly

sqlcommand cmd = new sqlcommand("select count(*) cntc_employee emp_alias= @alias", con); cmd.parameters.addwithvalue("@alias", this.alias.text); con.open();  if(convert.toint32(cmd.executescalar()) > 0) {                 errorprovider1.seterror(alias,"alias exist");     return true; } else {     return false; } 

but in 3 tier dont know how use executescalar in bll class in if ()section in bll class

private bool usernamecheck(string alias)  {     if (??)         throw new exception("alias exist");     else         return true; } 

in dal class

public void usernamecheck(string alias) {     string query6;     try     {         opencnn();          query6 = "select count(*) cntc_employee emp_alias= '" + alias + "' ";         cmd = new sqlcommand(query6, con);         cmd.executenonquery();     }     catch (exception ex)     {         throw ex;     }         {         closecnn();     } } 

lets start data access layer. while logic seems correct, argue way return objects. in opinion, you'd better return boolean indicating whether user exists or not. also, use parameterised queries, because don't want sqlinjection. see code:

// note: changed name more meaningfull use public static bool userexists(string alias) {     bool userexists = false;     try     {         // note: initialise cmd?         cmd.parameters.addwithvalue("alias", alias);         cmd.commandtext = "select count(*) cntc_employee emp_alias=@alias";         cmd.connection = con;         opencnn();         int amountofuserswithalias = (int)cmd.executescalar();         if(amountofuserswithalias > 0)               userexists = true;     }     catch (exception ex)     {         throw ex;     }         {         closecnn();     }     return userexists; } 

so have booleanvalue indicating if user exists. if returnvalue true, users exists. if false, user doesn't exist.

now, in businesslayer, call above method:

public bool usernamecheck(string alias) {     if (userclass.userexists(alias))                 return true     else                     return false; } 

you call static method correct alias. again, true or false gets returned, depending on whether user exists. use method in presentationlayer , show error user if true returned.

it may not perfect 3-tier architecture, job.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -