Dip
Dip

Reputation: 877

Retrieve value from database and assign it to any variable

My question is how to assign the data of the table to any variable.. I asked to retrieve forgotten password of employee through his email address inputed in textbox and send it to his mail account.. I already fetched the data but don't know how to assign that password in email body

my code is here

Model

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; }
    }
}

View

<%@ 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"/>

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();

//...... Below is the query where i fetch the password in var pwd

    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("[email protected]");
        MailAddress mailto = new MailAddress(user_email_address);
        MailMessage newmsg = new MailMessage(mailfrom, mailto);

        newmsg.Subject = "Your Password";

**// Here i assign pwd to messege body then it gives  an error of type casting so i converted it in To string**

        newmsg.Body = pwd.ToString();



        SmtpClient smtps = new SmtpClient("smtp.gmail.com", 587);
        smtps.UseDefaultCredentials = false;
        smtps.Credentials = new NetworkCredential("[email protected]", "password");
        smtps.EnableSsl = true;
        smtps.Send(newmsg);


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


   return View();
}

In newmsg.body i assign the variable pwd(which fetched the password of user) then it gives me an error of type casting so i use To String() method so it mails the sql query not password in the msg body.. So how to do it?

Upvotes: 0

Views: 4009

Answers (1)

LenPopLilly
LenPopLilly

Reputation: 1227

Try This

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

Upvotes: 1

Related Questions