Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#asp.net更新web应用程序中的用户信息_C#_Asp.net_Entity Framework - Fatal编程技术网

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

使用c#asp.net更新web应用程序中的用户信息,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我正在创建一个web应用程序,在此应用程序中,我希望允许用户更新其用户信息(名字、姓氏、电子邮件、电话号码、公司名称)或更改其密码。加载页面时,其当前信息将输入到文本框中。我的问题是,我不知道如何应用用户所做的更改。我的代码当前如下所示: DataClasses1DataContext context = new DataClasses1DataContext(); User user = new User(); protected void Page_Load(ob

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

    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); 
我收到了同样的错误信息

再看一下google,我首先找到了有关分离实体的信息,但这些信息已经存在了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");

      }

这是因为在更新之前,您将相同的信息写回这些文本框。您只需将页面加载更改为:

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

那对你来说应该行得通。让我知道!祝你好运

调试和逐步执行时,是否看到分配了正确的值?或者.Text属性是否仍包含分配的原始值?这些值分配正确,但缺少用户名。我把我为解决它所做的事情放在下面。这基本上就是我所做的…减去所有的if测试和try/catch块。它工作得非常好,谢谢你!很高兴解决了这个问题,你应该考虑使用错误捕捉。如果你不使用它,你的应用程序可能会抛出服务器错误。我添加了错误捕获。现在我只需要为它输入代码。