mysql - Returning value from Stored Procedure and using it in C# -
i have created function "recover(string useremail)" takes in user's email when he/she forgets login information. function runs stored procedure called "recover_password", return username , password can send email user. not familiar syntax creating simple stored procedure returns information. not familiar storing returned value in c# can make use of it.
recover_password:
create definer=`root`@`localhost` procedure `recover_password`(in email char(40)) begin select username,password userdata email=contactemail; end
recover(string useremail):
public actionresult recover(string useremail) { login model = new login(); mysqlconnection connect = dbconnection(); mysqlcommand cmd = new mysqlcommand("recover_password",connect); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@email", useremail); mysqldatareader rd = cmd.executereader(); while (rd.read()) { if (useremail == rd["contactemail"].tostring()) { mailmessage msg = new mailmessage(); msg.to.add(useremail); msg.subject = "recovered login information"; msg.body = "hello," + environment.newline + "your login information: "; //this want able use returned value } } return null; }
anyone have recommendations , advice on how can achieve this?
try code below. if works, please mark answer positive. thank you!
public actionresult recover(string useremail) { login model = new login(); mysqlconnection connect = dbconnection(); mysqlcommand cmd = new mysqlcommand("recover_password",connect); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@email", useremail); mysqldatareader rd = cmd.executereader(); while (rd.read()) { if (useremail == rd["contactemail"].tostring()) { try { smtpclient mysmtpclient = new smtpclient("my.smtp.exampleserver.net"); // set smtp-client basicauthentication mysmtpclient.usedefaultcredentials = false; system.net.networkcredential basicauthenticationinfo = new system.net.networkcredential("username", "password"); mysmtpclient.credentials = basicauthenticationinfo; mailmessage msg = new mailmessage(); msg.to.add(useremail); msg.subject = "recovered login information"; msg.body = "hello," + environment.newline + "your login information: "; //this want able use returned value msg.body += environment.newline + "username:" + (string)rd["username"]; msg.body += environment.newline + "password:" + (string)rd["password"]; mysmtpclient.send(msg); } catch (smtpexception ex) { throw new applicationexception ("smtpexception has occured: " + ex.message); } catch (exception ex) { throw ex; } } } return null; }
Comments
Post a Comment