我有一个需要更新记录的数据库表。添加新记录的代码可以正常工作,但是当我去更新现有记录时,并非所有字段都使用表单中的新信息进行更新。
这是代码:
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,其他一切都保持不变。没有错误或异常抛出或任何东西。像它一样工作,但没有。此外,当我硬编码要更新的值时,记录更新得很好。有任何想法吗?