Click here to Skip to main content
15,899,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

In my project i asked to send the forgotten password of user to his mail account..
for that i asked email address to user and fetched the password from database according to email address field.

my code is below..

code for View..

XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TelerikLogin.Models.ViewModels.ForgotPassword>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ForgotPassword
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>ForgotPassword</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

<% using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post))
   { %>

   <%: Html.LabelFor(m => m.user_email_address) %>
   <%: Html.TextBox("user_email_address")%>
      <%: Html.ValidationSummary(true) %>

<input type="submit" value="Submit"/>



code for Model..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace TelerikLogin.Models.ViewModels
{
    public class ForgotPassword
    {
        public int user_id { get; set; }
        public string user_login_name { get; set; }
        public string user_password { get; set; }

        [Required]
        [Display(Name="Email Address : ")]
        public string user_email_address { get; set; }
    }
}


code for Controller..

 public ActionResult ForgotPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgotPassword(string user_email_address)
        {
           
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MVC3\TelerikLogin\TelerikLogin\App_Data\Login.mdf;Integrated Security=True;User Instance=True");

            DataTable dt1 = new DataTable();
            
            string strQuery = string.Format("SELECT user_password FROM [user_master] WHERE user_email_address='{0}'",user_email_address);
           
  
          
            conn.Open();
        
            SqlDataAdapter da1 = new SqlDataAdapter(strQuery, conn);
            da1.Fill(dt1);
            
            LoginEntities3 le= new LoginEntities3();
          
           conn.Close();

// In below variable pwd i fetch the password...

            var pwd = from u in  new LoginEntities3().user_master
                       where u.user_email_address == user_email_address select u.user_password;


      

            if (dt1.Rows.Count > 0)
            {
                

                MailAddress mailfrom = new MailAddress("emailid@gmail.com");
                MailAddress mailto = new MailAddress(user_email_address);
                MailMessage newmsg = new MailMessage(mailfrom, mailto);

                newmsg.Subject = "Your Password";

// AND here i assign that variable to body

                newmsg.Body = pwd.ToString();

                

                SmtpClient smtps = new SmtpClient("smtp.gmail.com", 587);
                smtps.UseDefaultCredentials = false;
                smtps.Credentials = new NetworkCredential("emailid@gmail.com", "password");
                smtps.EnableSsl = true;
                smtps.Send(newmsg);
                

                return RedirectToAction("About", "Home");
            }
            

           return View();
        }



I have to send password in the body
so i assign variable pwd to msg.body
then it gave me an error of type casting so i use ToString method..
so it send an sql query instead of password..
:(

Any solution??
Posted
Updated 9-Mar-12 18:22pm
v2
Comments
member60 10-Mar-12 0:24am    
what about controller code?

1 solution

By placing the breakpoint be sure first that password is fetched.
Call Single or SingleOrDefault on your result set.try:
C#
var pwd =(from u in  new LoginEntities3().user_master
                      where u.user_email_address == user_email_address select u.user_password).SingleOrDefault();


and remove Tostring()from
C#
newmsg.Body = pwd.ToString();
 
Share this answer
 
v4
Comments
disha desani 10-Mar-12 1:13am    
Hey I tried as u said AND yes it displays the password.. ;)
Thanks
member60 10-Mar-12 1:21am    
happy coding.

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