我需要让用户更新输入到文本框中的数据,以反映回特定用户的详细信息到数据库中

时间:2016-10-24 08:20:29

标签: c# asp.net linq webforms

我的查询现在是我使用会话变量开发了一个用户主页,该会话变量将保存用户的姓名和用户的电子邮件,并将这些会话显示到主页aspx页面中,现在我需要让特定用户编辑和更新已注册的电子邮件,并在更新后,数据库还需要使用该特定用户的新电子邮件字段值进行更新。现在,在数据库中,我有一个名为tblcontacts的表,其中包含namefnameE-mailstudentID四列。请帮助我,如何找到并选择相关行以使用LINQ查询代码更新详细信息。



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Homepage.aspx.cs" Inherits="WebApplication5.Homepage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
&nbsp;&nbsp;&nbsp;<h1> Welcome!&nbsp; </h1>
        <p> &nbsp; <img src="user.jpg" /></p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;<br />
        <br />
&nbsp;Your Registered e-mail is:
        <asp:Label ID="lblmail" runat="server" Text="Label"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Edit" />
&nbsp;&nbsp;&nbsp;&nbsp; New-Email :&nbsp;
        <asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
        &nbsp;
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="Wrong Format!" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
        <br />
        <br />
        <br />
        <br />
    
    </div>
    </form>
</body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
    public partial class Homepage : System.Web.UI.Page
    {
        public static int check = 0;
        DataClasses1DataContext db = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            lblName.Text = Session["name"].ToString();
            lblmail.Text = Session["email"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            tblContact tb = new tblContact();
            check += 1;
            if (check % 2 == 0)
            { 
              
                tb.Email = TextBox1.Text;
                TextBox1.Visible = false;
                Button1.Text = "edit";
                db.SubmitChanges();
            }
            else
            {
                TextBox1.Visible = true;
                Button1.Text = "Update";
            }
        }
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在对所需代码进行一些调查后,我通过删除代码中的行作为tb.email = textbox1.text并在按钮单击事件处理函数中添加以下两行代码后修复此问题:

 protected void Button1_Click(object sender, EventArgs e)
    {
        tblContact tb = new tblContact();
        check += 1;
        if (check % 2 == 0)
        { 
           var student = (from c in db.tblContacts where c.name == lblName.Text select c).Single();
           student.Email = TextBox1.Text;
            TextBox1.Visible = false;
            Button1.Text = "edit";
            db.SubmitChanges();
        }
        else
        {
            TextBox1.Visible = true;
            Button1.Text = "Update";
        }
    }

LINQ查询中的单个方法使得它可以在表格中选择要更新详细信息的所需行!此答案必须标记正确,因为这通过查询解决