【问题标题】:Retrieve value from database and assign it to any variable从数据库中检索值并将其分配给任何变量
【发布时间】:2012-03-10 05:06:01
【问题描述】:

我的问题是如何将表的数据分配给任何变量.. 我要求通过他在文本框中输入的电子邮件地址找回忘记的员工密码并将其发送到他的邮件帐户。 我已经获取了数据,但不知道如何在电子邮件正文中分配该密码

我的代码在这里

型号

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 查询而不是密码.. 那么该怎么做呢?

【问题讨论】:

  • 您能否调试并验证“pwd”的值是多少?
  • 我猜你需要在你的选择 LINQ 查询上使用 singleordefault msdn.microsoft.com/en-us/library/bb359429.aspx
  • @Manas oh okey 我调试它.. 它只显示像这样的表达式 ((System.Data.Objects.ObjectQuery)(pwd))
  • 存储密码几乎总是错误的做法——如果可以的话,挑战这种设计。

标签: asp.net asp.net-mvc-3 c#-4.0


【解决方案1】:

试试这个

string pwd = (来自 u in new LoginEntities3().user_master 其中 u.user_email_address == user_email_address 选择 u.user_password).ToString();

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 2018-09-03
    • 2023-03-31
    • 1970-01-01
    • 2020-08-05
    • 2021-08-13
    • 2018-11-13
    • 2020-12-23
    • 2017-07-26
    相关资源
    最近更新 更多