hi!! i need to send an email to the user who has forgot his/her password... but i am not able to send email.. it is giving me an error.."Invalid
attempt to read when no data is present" i think it is the problem of sqldatareader but i am not getting what exactly the problem is... i have set the mail setting in web.config file....
below is the code....
protected void passforgot_submit_btn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(my connection string name);
//check the email-id with the database
try
{
con.Open();
string cmd = "SELECT u_email_id FROM users_detail WHERE u_email_id='" + pasrec_txt.Text + "'";
SqlCommand cmdd = new SqlCommand(cmd, con);
SqlDataReader reader = cmdd.ExecuteReader();
//this will check the email-id with the database,if present will send an email
string link = "website name?resetkey=" + key + "&token=" + expiration;
string add = "Reset Your Password";
//email message for password reset
.....
MAIL CODE
......
// Send the email
SmtpClient client = new SmtpClient();
client.Send(mail);
client.EnableSsl = true;
string em = pasrec_txt.Text;
string msg = "An email has been sent to" + em + "which is the registered address for your account. <br/> It includes information on changing and confirming your new password.";
error.Text = msg;
}
// if not present will display an error
else
{
string err = "Sorry!! No account with that e-mail address exists.";
You need to call DataReader.Read to get access to the first record. It will return true if there is a record available. Do not close the connection before you are done reading in data with the datareader.
ankitk
Member
4 Points
50 Posts
Invalid attempt to read when no data is present
Apr 08, 2012 05:53 PM|LINK
hi!! i need to send an email to the user who has forgot his/her password... but i am not able to send email.. it is giving me an error.."Invalid attempt to read when no data is present" i think it is the problem of sqldatareader but i am not getting what exactly the problem is... i have set the mail setting in web.config file....
below is the code....
protected void passforgot_submit_btn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(my connection string name);
//check the email-id with the database
try
{
con.Open();
string cmd = "SELECT u_email_id FROM users_detail WHERE u_email_id='" + pasrec_txt.Text + "'";
SqlCommand cmdd = new SqlCommand(cmd, con);
SqlDataReader reader = cmdd.ExecuteReader();
//this will check the email-id with the database,if present will send an email
if(reader["u_email_id"].ToString() == pasrec_txt.Text)
{
con.Close();
DateTime expiration = DateTime.Now.AddDays(1);
con.Open();
SqlCommand commd = new SqlCommand("SELECT u_security.user_guid FROM u_security INNER JOIN users_detail ON u_security.u_id = users_detail.u_id where users_detail.u_email_id = '" + @pasrec_txt.Text + "'", con);
SqlDataReader reader1 = commd.ExecuteReader();
reader1.Read();
string key = reader1.GetGuid(0).ToString();
reader1.Close();
con.Close();
string link = "website name?resetkey=" + key + "&token=" + expiration;
string add = "Reset Your Password";
//email message for password reset
.....
MAIL CODE
......
// Send the email
SmtpClient client = new SmtpClient();
client.Send(mail);
client.EnableSsl = true;
string em = pasrec_txt.Text;
string msg = "An email has been sent to" + em + "which is the registered address for your account. <br/> It includes information on changing and confirming your new password.";
error.Text = msg;
}
// if not present will display an error
else
{
string err = "Sorry!! No account with that e-mail address exists.";
error.Text = err;
}
}
catch (Exception ex)
{
error.Text = ex.Message.ToString();
}
CAN SOMEONE EXPLAIN ME HOW EFFICIENTLY WE CAN USE DATAREADER ....ACTUALLY I DON'T HAVE MUCH EXPIRENCE IN ADO.NET
thank's in advance... :)
Ken Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: Invalid attempt to read when no data is present
Apr 08, 2012 05:57 PM|LINK
You need to call DataReader.Read to get access to the first record. It will return true if there is a record available. Do not close the connection before you are done reading in data with the datareader.
Space Coast .Net User Group