3

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

    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 类下的所有字段设置为 never 用于更新策略。有人建议反序列化然后再次序列化数据,但我找不到有关如何执行此操作的任何信息。任何帮助,将不胜感激。

编辑:由于下面的一些建议而更改了代码,但它仍然无法正常工作。我没有收到任何错误消息,但没有保存数据。

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

      }
4

1 回答 1

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

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

于 2012-04-16T13:34:34.803 回答