c# - I want to check the username is already exists in my database table or not? -
i have registration page , want check username exist in database or not in 3 tier architecture.
myregistration.cs:
public static int checkusername(string user_txt) { int id2 = 0; string selectstr = "select * xyz username = '" + user_txt + " ' "; id2 = dataaccesslayer.executereader(selectstr); return id2; }
and code behind onclick event of textbox:
protected void txt_username_textchanged(object sender, eventargs e) { if (!string.isnullorempty(txt_username.text)) { int id = xyz.checkusername(txt_username.text.trim()); if (id > 0) { lblstatus.text = "username taken"; } else { lblstatus.text = "username available"; } } }
dataaccesslayer:
public static int executereader(string query) { sqlconnection con = new sqlconnection(); con.connectionstring = getconnectionstring(); con.open(); int id = 0; sqlcommand cmd = new sqlcommand(); cmd.commandtext = query; cmd.commandtype = system.data.commandtype.text; cmd.connection = con; sqldatareader reader = cmd.executereader(); while (reader.read()) { id++; } cmd = null; reader.close(); con.close(); return id; }
i have edited of codes try below... you...
text change event :
protected void txt_username_textchanged(object sender, eventargs e) { if (!string.isnullorempty(txt_username.text)) { if (xyz.checkusername(txt_username.text.trim())) { lblstatus.text = "username taken"; } else { lblstatus.text = "username available"; } } }
check username :
public bool checkusername(string user_txt) { bool result; result = dataaccesslayer.executereader(user_txt); return result; }
excute reader :
public bool executereader(string user_txt) { sqlconnection con = new sqlconnection(); con.connectionstring = getconnectionstring(); con.open(); sqlcommand cmd = new sqlcommand("select * xyz username = @userid", con); sqlparameter param = new sqlparameter(); param.parametername = "@userid"; param.value = user_txt; cmd.parameters.add(param); sqldatareader reader = cmd.executereader(); if (reader.hasrows) return true; else return false; }
Comments
Post a Comment