【问题标题】:Trying to update a record, but the update code only updates one of the timestamp fields and nothing else尝试更新记录,但更新代码仅更新时间戳字段之一,没有其他内容
【发布时间】:2013-07-31 13:48:39
【问题描述】:

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

代码如下:

    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,其他一切都保持不变。没有错误或异常抛出或任何东西。像它一样工作,但没有。此外,当我硬编码要更新的值时,记录更新得很好。有什么想法吗?

【问题讨论】:

  • 调试你的代码并检查是否有你想要更新的值。
  • 如果您使用的是Visual Studio,请尝试在方法顶部设置断点并右键单击-> watch oldDSN。检查所有值是否正确更新。我无法无礼地这么说:因为唯一要更新的值是您的代码未生成的值(DateTime.Now),我觉得很可能是您的代码生成了不正确的值。
  • 我现在正在单步执行代码。只需一分钟,我会更新并让您知道发生了什么。
  • 好的,发生的事情是:代码看起来工作得很好,但是当它从 HTML 元素(文本框等)中获取文本时,它不会获取新输入的信息,它抓取 page_load 运行时放入文本框中的内容,当然,这是旧值,因此看起来好像没有更新它们......我很困惑为什么它不会抓取不过,只是输入了它们。

标签: c# asp.net sql-server linq-to-sql


【解决方案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
    }
}

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

【讨论】:

  • 谢谢你。我在工作中被困在这里一两天,无法弄清楚。花了很长时间看我的更新代码,甚至没有想到看我的这部分代码。再次感谢!
  • @ChrisHansen 很高兴我能帮上忙!
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
相关资源
最近更新 更多