我的问题是如何将表的数据分配给任何变量.. 我要求通过他在文本框中输入的电子邮件地址检索员工忘记密码并将其发送到他的邮件帐户。 我已经提取了数据,但不知道如何在电子邮件正文中分配该密码
我的代码在这里
模型
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("myid@gmail.com");
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("myid@gmail.com", "password");
smtps.EnableSsl = true;
smtps.Send(newmsg);
return RedirectToAction("About", "Home");
}
return View();
}
在newmsg.body中 我分配变量pwd(获取用户的密码)然后它给我一个类型转换错误所以我使用To String()方法 所以它在msg正文中邮寄sql查询而不是密码.. 那怎么办呢?
答案 0 :(得分:0)
试试这个
字符串 pwd =(来自新的LoginEntities3()中的u.user_master 其中u.user_email_address == user_email_address选择u.user_password) .ToString();