我的查询现在是我使用会话变量开发了一个用户主页,该会话变量将保存用户的姓名和用户的电子邮件,并将这些会话显示到主页aspx页面中,现在我需要让特定用户编辑和更新已注册的电子邮件,并在更新后,数据库还需要使用该特定用户的新电子邮件字段值进行更新。现在,在数据库中,我有一个名为tblcontacts
的表,其中包含name
,fname
,E-mail
和studentID
四列。请帮助我,如何找到并选择相关行以使用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>
<br />
<h1> Welcome! </h1>
<p> <img src="user.jpg" /></p>
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
<br />
<br />
Your Registered e-mail is:
<asp:Label ID="lblmail" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Edit" />
New-Email :
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<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;
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;
答案 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查询中的单个方法使得它可以在表格中选择要更新详细信息的所需行!此答案必须标记正确,因为这通过查询解决