gpt4 book ai didi

c# - 使用 c# asp.net 更新 Web 应用程序中的用户信息

转载 作者:行者123 更新时间:2023-11-30 18:00:18 24 4
gpt4 key购买 nike

我正在创建一个网络应用程序,在这个应用程序中,我希望允许用户更新他们的用户信息(名字、姓氏、电子邮件、电话号码、公司名称)或更改他们的密码。当页面加载时,他们的当前信息被输入到文本框中。我的问题是我不知道如何应用用户所做的更改。我的代码目前看起来像这样:

    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");

}

最佳答案

发生这种情况是因为您在更新之前将相同的信息写回了那些文本框。您需要做的就是将您的 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
}
}

这应该适合您。让我知道!祝你好运!

关于c# - 使用 c# asp.net 更新 Web 应用程序中的用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10175022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com