2012-03-10 56 views
0

我的问题是如何将表中的数据分配给任何变量.. 我要求通过他在邮箱中输入的电子邮件地址检索员工忘记的密码并将其发送给他邮件帐户.. 我已经获取的数据,但不知道如何在电子邮件正文中分配一个密码从数据库中检索值并将其分配给任何变量

我的代码是在这里

型号

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

查看

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

控制器

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

在newmsg.body 我分配变量PWD(其取出的用户的口令),则它给我型铸造这样的误差我使用To String()方法 所以它邮件的MSG身体的SQL查询不密码。 那么如何做到这一点?

+0

您可以请调试并验证“pwd”的值是什么? – Manas 2012-03-10 05:22:12

+0

我想你需要在你选择的LINQ查询中使用singleordefault http://msdn.microsoft.com/en-us/library/bb359429.aspx – 2012-03-10 05:22:36

+0

@Manas oh okey我调试它..它只显示像这样的表达式 ((System.Data.Objects.ObjectQuery)(pwd)) – Dip 2012-03-10 05:30:41

回答

0

尝试这个

PWD =(从u在新LoginEntities3()user_master 其中u.user_email_address == USER_EMAIL_ADDRESS选择u.user_password。)的ToString();

相关问题