Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir, I am using forgot password module in asp.net.

The thing is that I getting password from backend if email is right that I written in textbox. But if I put wrong email id the program will give eror - the eror is there is not row at position at zero position.


How will i overcome from it? Here is code

public string  forgotpassword(string mail)
           {

               string result="";
             
               SqlConnection con = new SqlConnection(connstr);
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }
               con.Open();
               SqlCommand cmd = new SqlCommand("select password from tbluser where mail=@mail", con);
             
              cmd.Parameters.Add("@mail", SqlDbType.VarChar, 50).Value = mail;
              DataSet ds = new DataSet(); 
              SqlDataAdapter adp = new SqlDataAdapter(cmd);
             
              adp.Fill(ds);

              if (Convert.ToString(ds.Tables[0].Rows[0]["password"]) != null) 
              {
                  return result = Convert.ToString(ds.Tables[0].Rows[0]["password"]);
               
              }
              return result; 
              
           }
Posted
Updated 8-Oct-10 20:23pm
v2

1 solution

If no rows are returned from the query, then simply show a message saying this email does not exist.

Another way could be to first check if that emails exists in the tbluser by using a query, and then, if this email is found, then continue to check the password.
 
Share this answer
 
Comments
balongi 9-Oct-10 2:32am    
i am showing it on label if email does not exist but it not work .. here is code

protected void BtnSubmit_Click(object sender, EventArgs e)
{






if (TextBox1.Text == "")
{
Lblpwd.Text = "wrong mail id";


}
else
{
mydatalayer.DataLayer d1 = new mydatalayer.DataLayer();
d1.forgotpassword(TextBox1.Text);
Lblpwd.Text = "your password is: " + d1.forgotpassword(TextBox1.Text);
}





}
Abhinav S 9-Oct-10 2:35am    
A better way to this would be to check the row count of your data adapter and if it is zero, then obviously the email was not found in the database.
balongi 9-Oct-10 2:38am    
how i would check plz suggest i am beginer for asp.net
balongi 9-Oct-10 2:43am    
thanx it work by your suggestion .. it work now by adding these

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

return result = Convert.ToString(ds.Tables[0].Rows[0]["password"]);

}
else
{
return result;
}

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