1

我有一个需要更新记录的数据库表。添加新记录的代码可以正常工作,但是当我去更新现有记录时,并非所有字段都使用表单中的新信息进行更新。

这是代码:

    private void updateExistingDSN()
    {
        //Update existing DSN
        try
        {
            using (PathFinderDataContext pfdcContext = new PathFinderDataContext())
            {
                DSN oldDSN = pfdcContext.DSNs.Single(dsn => dsn.DSNID == int.Parse(Request["dsn"]));

                oldDSN.Auth_AuthorizationID = int.Parse(Request["auth"]);
                oldDSN.ServiceProvided_ServiceProvidedID = int.Parse(Request["sp"]);
                oldDSN.EvidenceBPMU = short.Parse(ddlEvidenceBPMU.SelectedValue);
                oldDSN.LocationOfVisit = txtLocationOfVisit.Text;
                oldDSN.ChildrenPresent = txtNamesOfChildrenPresent.Text;
                oldDSN.ParentPresent = txtNamesOfParentsPresent.Text;
                oldDSN.OthersPresent = txtNamesOfOthersPresent.Text;
                oldDSN.DescribeGoals = txtDescribeGoals.Text;
                oldDSN.DescribeStrategy = txtDescribeStrategies.Text;
                oldDSN.DescibeParentingSkills = txtDescribeParentingSkills.Text;
                oldDSN.DescribeSafetyConcerns = txtDescribeSafetyConcerns.Text;
                oldDSN.OtherInfo = txtOtherInfo.Text;
                oldDSN.Schedule_Monday = float.Parse(txtMonday.Text);
                oldDSN.Schedule_Tuesday = float.Parse(txtTuesday.Text);
                oldDSN.Schedule_Wednesday = float.Parse(txtWednesday.Text);
                oldDSN.Schedule_Thursday = float.Parse(txtThursday.Text);
                oldDSN.Schedule_Friday = float.Parse(txtFriday.Text);
                oldDSN.Schedule_Saturday = float.Parse(txtSaturday.Text);
                oldDSN.Schedule_Sunday = float.Parse(txtSunday.Text);
                oldDSN.DateSaved = DateTime.Now;
                oldDSN.SavedBy_UserID = currentEmployee.EmployeeID;
                pfdcContext.SubmitChanges();
            }

            Response.Redirect("~/pages/updateTimesheet.aspx?action=update&ProvidedServiceId=" + int.Parse(Request["sp"]));
        }
        catch (Exception ex)
        {
            errorMessage.Text = "<b>Error updating an existing DSN record!</b><br /><br />" + ex.ToString();
            warnings.Visible = true;
        }
    }

唯一更新的字段是 oldDSN.DateSaved,其他一切都保持不变。没有错误或异常抛出或任何东西。像它一样工作,但没有。此外,当我硬编码要更新的值时,记录更新得很好。有任何想法吗?

4

1 回答 1

1

在您的Page_Load(您将数据源中的信息放入文本框等的地方)中,您需要将数据绑定代码包装在一个If(Page.IsPostBack)块中。

所以你的代码应该是这样的:

protected void Page_Load (object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        // Whatever you use to load the data from the database into 
        // your server controls goes here
        loadData(); // example
    }
}

这就是您没有获得更新信息的原因 - 在您的更新代码有机会运行之前,您的标记元素正在从数据库中重新加载。

于 2013-07-31T14:45:41.483 回答