我正在创建一个网络应用程序,在这个应用程序中,我想允许用户更新他们的用户信息(名字,姓氏,电子邮件,电话号码,公司名称)或更改他们的密码。页面加载时,它们的当前信息将输入到文本框中。我的问题是我不知道如何应用用户所做的更改。我的代码目前看起来像这样:
DataClasses1DataContext context = new DataClasses1DataContext();
User user = new User();
protected void Page_Load(object sender, EventArgs e)
{
string usr = Session["SessionUserName"].ToString();
user = (from u in context.Users
where u.username.Equals(usr)
select u).FirstOrDefault();
txtFName.Text = user.firstName;
txtLName.Text = user.lastName;
txtCompany.Text = user.companyName;
txtEmail.Text = user.email;
txtPhone.Text = user.phoneNumber;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
using (DataClasses1DataContext context2 = new DataClasses1DataContext())
{
User nUser = (from u in context2.Users
where u.username == user.username
select u).SingleOrDefault();
if (nUser != null)
{
//make the changes to the user
nUser.firstName = txtFName.Text;
nUser.lastName = txtLName.Text;
nUser.email = txtEmail.Text;
if (!String.IsNullOrEmpty(txtPhone.Text))
nUser.phoneNumber = txtPhone.Text;
if (!String.IsNullOrEmpty(txtCompany.Text))
nUser.companyName = txtCompany.Text;
nUser.timeStamp = DateTime.Now;
}
//submit the changes
context2.SubmitChanges();
Response.Redirect("Projects.aspx");
}
这不会给我任何错误,但它也不会应用这些更改。一些谷歌搜索引导我添加
context2.Users.Attach(nUser);
但是这给了我错误消息:System.InvalidOperationException:无法附加已存在的实体。所以我试过
context2.Users.Attach(nUser, true);
我收到了同样的错误消息。
更多地看谷歌我发现了有关首先分离实体的信息,但该信息大约有3年的历史,并且似乎不再有效(因为context.Detach或context.Users.Detach不是选项并且给出如果我尝试使用它,我会构建错误)。我收到的其他错误消息(我不记得我是如何诚实地)建议我更改更新策略,因此我将Users类下的所有字段设置为永远不会更新策略。有人建议再次反序列化然后再序列化数据,但我找不到任何有关如何执行此操作的信息。任何帮助将不胜感激。
编辑:由于下面的一些建议而更改了代码,但它仍然无效。我没有收到任何错误消息,但数据没有被保存。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string usr = Session["SessionUserName"].ToString();
user = (from u in context.Users
where u.username.Equals(usr)
select u).FirstOrDefault();
txtFName.Text = user.firstName;
txtLName.Text = user.lastName;
txtCompany.Text = user.companyName;
txtEmail.Text = user.email;
txtPhone.Text = user.phoneNumber;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
//make the changes to the user
user.firstName = txtFName.Text;
user.lastName = txtLName.Text;
user.email = txtEmail.Text;
if (!String.IsNullOrEmpty(txtPhone.Text))
user.phoneNumber = txtPhone.Text;
if (!String.IsNullOrEmpty(txtCompany.Text))
user.companyName = txtCompany.Text;
user.timeStamp = DateTime.Now;
//submit the changes
context.SubmitChanges();
Response.Redirect("Projects.aspx");
}
答案 0 :(得分:1)
这种情况正在发生,因为您在更新之前将相同的信息写回这些文本框。您需要做的就是将page_load更改为:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string usr = Session["SessionUserName"].ToString();
user = (from u in context.Users
where u.username.Equals(usr)
select u).FirstOrDefault();
txtFName.Text = user.firstName;
txtLName.Text = user.lastName;
txtCompany.Text = user.companyName;
txtEmail.Text = user.email;
txtPhone.Text = user.phoneNumber;
}
}
在更新之前,page_load正在执行。因此,它重新分配原始值,然后使用相同的值更新它们。希望这可以帮助!祝你好运!
编辑:问题后的更改已更新:
将您的整个代码更改为此。我还添加了一些需要检查,你应该处理并从顶部删除全局变量:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["SessionUserName"] != null)
{
string usr = Session["SessionUserName"].ToString();
DataClasses1DataContext context = new DataClasses1DataContext();
User user = (from u in context.Users
where u.username.Equals(usr)
select u).FirstOrDefault();
if (user != null)
{
txtFName.Text = user.firstName;
txtLName.Text = user.lastName;
txtCompany.Text = user.companyName;
txtEmail.Text = user.email;
txtPhone.Text = user.phoneNumber;
}
else
{
//--- handle user not found error
}
}
else
{
//--- handle session is null
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (Session["SessionUserName"] != null)
{
string usr = Session["SessionUserName"].ToString();
try
{
DataClasses1DataContext context = new DataClasses1DataContext();
User nUser = (from u in context.Users
where u.username.Equals(usr)
select u).FirstOrDefault();
//make the changes to the user
nUser.firstName = txtFName.Text;
nUser.lastName = txtLName.Text;
nUser.email = txtEmail.Text;
if (!String.IsNullOrEmpty(txtPhone.Text))
nUser.phoneNumber = txtPhone.Text;
if (!String.IsNullOrEmpty(txtCompany.Text))
nUser.companyName = txtCompany.Text;
nUser.timeStamp = DateTime.Now;
//submit the changes
context.SubmitChanges();
Response.Redirect("Projects.aspx");
}
catch (Exception ex)
{
//--- handle update error
}
}
else
{
//--- handle null session error
}
}
这应该适合你。让我知道!祝你好运!