【问题标题】:Linq update queryLinq 更新查询
【发布时间】:2012-08-28 11:06:36
【问题描述】:

我想更新现有记录。我也调试了我的代码。 int id 变量获取值,但不知道为什么我的记录没有更新。

SQLDBDataClassesDataContext dContext = new SQLDBDataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
    //if (!IsPostBack)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["fname"]))
        {
            FirstNameTextBox.Text = Request.QueryString["fname"];
        }
        if (!string.IsNullOrEmpty(Request.QueryString["lname"]))
        {
            LastNameTextBox.Text = Request.QueryString["lname"];
        }
        if (!string.IsNullOrEmpty(Request.QueryString["cellnum"]))
        {
            CellNumberTextBox.Text = Request.QueryString["cellnum"];
        }
    }
}

protected void UpdateButton_Click(object sender, EventArgs e)
{
    int id = Convert.ToInt32(Request.QueryString["id"]);
    var updatequery1 = dContext.PersonalDetailTables
        .Where(pd => pd.ID == id).SingleOrDefault();

    if (updatequery1 != null)
    {
        updatequery1.FirstName = FirstNameTextBox.Text;
        updatequery1.LastName = LastNameTextBox.Text;
        updatequery1.CellNumber = CellNumberTextBox.Text;
        dContext.SubmitChanges();
    }
}

【问题讨论】:

  • 尝试在该部分的 if 和 else 中显示一条消息,看看显示的是什么消息,这样你就知道它是否进入了 if 语句。
  • 您确定正确使用了 DataContext 吗?从您的代码中不清楚什么是上下文以及它来自哪里。
  • @SynerCoder.. 它进入 if(updatequery1!= null) 子句.. 但在这里,值保持不变并且没有改变

标签: c# asp.net linq


【解决方案1】:

if (updatequery1 != null)

updatequery1 是否为空?如果是现在,它会直接跳过。设置断点并单步执行。

或者,将 .SingleOrDefautl 更改为 .Single。这样,如果什么都没找到,你会得到一个异常,你就会知道发生了什么。

【讨论】:

  • 嗯,我将 singleordefault 更改为 .single。但有一件事,updatequery1 不为空。它进入 if 子句。 什么都没有发生 :/
【解决方案2】:

在调用dContext.SubmitChanges();之前调用dContext.InsertOnSubmit(fooentity)方法。

例如。

 var ctx = DB.fooDB;
        fooobj.fooName= bar.fooName;
        fooobj.fooID= bar.fooID;
        if (fooobj.fooID== 0)
            ctx.Locations.InsertOnSubmit(bar);
        ctx.SubmitChanges();

【讨论】:

    【解决方案3】:

    我有我的解决方案.. 唯一的问题是 if(!ispostback) 问题。

    【讨论】:

      【解决方案4】:
      context.InvProductStockMasters
             .Update(ps => ps.LocationID.Equals(invStockAdjustmentHeader.LocationID) 
                        && ps.ProductID.Equals(invStockAdjustmentDetail.ProductID), 
                     ps => new InvProductStockMaster 
                                { Stock = ps.Stock - invStockAdjustmentDetail.OrderQty });
      

      【讨论】:

        猜你喜欢
        • 2018-06-04
        • 2018-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        • 2013-03-26
        相关资源
        最近更新 更多