如何从数据库中获取数据并将其分配给Asp.Net C#MVC-3中的任何变量? [英] How to fetch the data from database and assign it to any variable in Asp.Net C# MVC-3?

查看:287
本文介绍了如何从数据库中获取数据并将其分配给Asp.Net C#MVC-3中的任何变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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..

<%@ 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..

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




我必须在正文中发送密码
所以我将变量pwd分配给msg.body
然后它给了我类型转换错误,所以我使用了ToString方法..
因此它发送一个sql查询而不是密码..
:(

任何解决方案??




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??

推荐答案

通过放置断点,请确保首先获取密码.
在结果集上调用Single或SingleOrDefault.try:
By placing the breakpoint be sure first that password is fetched.
Call Single or SingleOrDefault on your result set.try:
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

newmsg.Body = pwd.ToString();


这篇关于如何从数据库中获取数据并将其分配给Asp.Net C#MVC-3中的任何变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆