Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi sir,I have been designing a project int that i have to write code for forget password i wrote some code and i check that code by putting some debug point.while debugging the password from data base is reading into the data access layer but while leaving the loop from data access layer it showing null.I tried but i couldnt get .here i am giving my code which i tried.

in database my code is:

Create procedure sp_ForgetPwd(
@UserName varchar(20),
@SecurityQuetion varchar(Max) ,
@Answer varchar(Max))
as
begin
select Password from tb_Login where UserName=@UserName And SecurityQuetion=@SecurityQuetion and Answer=@Answer
end

in Presentation layer the code is:

protected void Button1_Click(object sender, EventArgs e)
{
string username = TextBox2.Text;
string securityquestion = DropDownList1.SelectedItem.Text;
string ans = TextBox5.Text;
string pwd = "";

Session["pwd"] = pwd;
b.Forgetpwd(username, securityquestion , ans, pwd);

Label1.Text = "Your Password is " + Session["pwd"];
}

in DAta Access layer the code is:


public string Forgetpwd(string username,string securityquestion ,string ans,string pwd)
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_ForgetPwd", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", username);
cmd.Parameters.AddWithValue("@SecurityQuetion", securityquestion );
cmd.Parameters.AddWithValue("@Answer", ans);
try
{

SqlDataAdapter adapter = new SqlDataAdapter(cmd);


DataSet ds = new DataSet();
adapter.Fill(ds);
SqlDataReader dr = cmd.ExecuteReader();


if (ds.Tables[0].Rows.Count>0)

{

pwd = ds.Tables[0].Rows[0]["Password"].ToString();



}
}
catch (Exception ex)
{
pwd = "";
}

return pwd;
}


in Business layer the code is:

public string Forgetpwd(string username,string securityquestion ,string ans,string pwd)
{

return d.Forgetpwd(username, securityquestion , ans,pwd);
}


Please anyone help me to solve this problem.
And Thanks in Advance.

What I have tried:

i Have tried above code and tried with some other way but the problem is still remains.
Posted
Comments
ZurdoDev 2-Jun-17 7:12am    
It's probably hitting the exception where you set pwd = "". Just keep debugging. We can't run your code but that's what we would do if we were in your position. Find it.
F-ES Sitecore 2-Jun-17 7:12am    
What is this line for?

SqlDataReader dr = cmd.ExecuteReader();

Are you sure your code isn't going into your "catch" block as an error is thrown? When you debug is the password in "pwd" ok?

As a side note this whole concept is a bad idea, you should never store passwords in plain text or allow people to recover them. Storing a password in the Session is also a very bad idea.
Member 13237087 3-Jun-17 3:39am    
Yes it is not hitting the catch block.Until it reaches the "return pwd" it is showing the password after it leaves the final block(}) it goes to Business Layer there it is showing null.
Richard Deeming 2-Jun-17 9:18am    
Never store password in plain text!

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

And never allow the user to "recover" their password; if they can't remember it, then reset it instead.

Troy Hunt: Everything you ever wanted to know about building a secure password reset feature[^]

You're also trying to reinvent the wheel - ASP.NET has several perfectly good authentication / authorization systems built-in. For example, ASP.NET Identity[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900