c# - how to use the return value of stored procedure in code to verify -
c# - how to use the return value of stored procedure in code to verify -
here sql server stored procedure verify user credentials.
alter procedure [dbo].[validateusercredentials] @username varchar(50), @password varchar(50), @result varchar(50) out begin if exists(select username, password users username = @username , password = @password) begin set @result = 1 end else begin set @result = 0 end end
here method in reuse class.
public bool executeinsertprocedure(string spname, sqlparameter[] sqlparameters, out string message) { sqlcommand cmd = new sqlcommand(); cmd.connection = conn; cmd.commandtype = commandtype.storedprocedure; cmd.commandtext = spname; cmd.parameters.addrange(sqlparameters); conn.open(); seek { cmd.executenonquery(); message = sqlparameters[sqlparameters.length - 1].value.tostring(); homecoming true; } grab { message = "something wrong!"; homecoming false; } }
here calling in bll.
public void verifyusercredentials(string username, string password,string message,out string sp_message) { sqlparameter[] param = new sqlparameter[3]; param[0] = new sqlparameter("@username", sqldbtype.varchar); param[0].value = username; param[1] = new sqlparameter("@password", sqldbtype.varchar); param[1].value = password; param[2] = new sqlparameter("@result", sqldbtype.varchar); param[2].direction = parameterdirection.output; //param[2].value = sp_message; objruse = new reuse(); objruse.executeinsertprocedure("validateusercredentials", param, ??); }
and here login page.
protected void btnsignin_click(object sender, eventargs e) { objbll = new bll(); objbll.verifyusercredentials(txtsigninusername.text,txtsigninpassword.text, ??); if(?? == true) { session["username"]=txtsigninusername; response.redirect("default.aspx"); } else { response.redirect("login.aspx"); } }
what should set in ?? places in code. how returned value of stored procedure.
change procedure have @result int out
or @result bit out
instead of varchar(50)
change method executeinsertprocedure
public bool executeinsertprocedure(string spname, sqlparameter[] sqlparameters, out int message) { seek { cmd.executenonquery(); message = cmd.parameters["@result"].value; (message == 1) ? homecoming true : homecoming false; } }
your verifyusercredentials
method
public void verifyusercredentials(string username, string password,string message,out int sp_message) { objruse = new reuse(); objruse.executeinsertprocedure("validateusercredentials", param, sp_message); }
your btnsignin_click
event
protected void btnsignin_click(object sender, eventargs e) { int errorid; objbll = new bll(); if(objbll.verifyusercredentials(txtsigninusername.text,txtsigninpassword.text, out errorid)) { session["username"]=txtsigninusername; response.redirect("default.aspx"); } else { //else part code } }
c# sql sql-server
Comments
Post a Comment