how to check email is already there in database or not

Posted by reddysankark-13471 under ASP.NET on 4/29/2012 | Points: 10 | Views : 2865 | Status : [Member] | Replies : 1
hi

coding for forget password through gmail is working fine my question is how to check wheather email id is there in database or not if its not there execute please register first. how write coding for this
coding
using (SqlConnection con = new SqlConnection(_constr))
{
string query = "select Password,email from login where email='" + txtemail.Text + "'";
SqlCommand cmd = new SqlCommand(query, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int rowcount = ds.Tables[0].Rows.Count;
if (rowcount > 0)
{
email = ds.Tables[0].Rows[0]["email"].ToString();
password = ds.Tables[0].Rows[0]["password"].ToString();

MailMessage mail = new MailMessage();
mail.From = new MailAddress("gmailid");
mail.To.Add(email);
mail.Subject = "Reg. Password ";
mail.Body = " Your Password for the user" + " " + txtemail.Text.ToString() + " " + " is " + password;
mail.Priority = System.Net.Mail.MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("gmailid", "password");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
//lbl6.Text = " The password has successfully sent to " + email;
client.Send(mail);
}
}


my database coding is:

CREATE PROCEDURE CheckUserName( @email VARCHAR(50) )
AS
BEGIN

IF EXISTS (SELECT COUNT(email) FROM email WHERE [email] = @email )

SELECT '1'; --user name already exist in database

ELSE

SELECT '0'; --user name does not exist in database

END

sankarreddy


Responses

Posted by: Johnbhatt on: 7/14/2012 [Member] Starter | Points: 25

Up
0
Down
So, you have a good code, what is your problem. Check Stored Procedure before Sending a mail.
ie.
First Check that if email id exists or not which you have done in below step.
int rowcount = ds.Tables[0].Rows.Count;

if (rowcount > 0)

Now just place an Else block and print a message, "This Email ID is not associated with us, Register here."



John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response