代码之家  ›  专栏  ›  技术社区  ›  Missy

并发错误WinForms绑定源导航器

  •  0
  • Missy  · 技术社区  · 7 年前

    我有一个包含客户信息的表单,每页需要处理一个事务。我正在使用绑定导航器来管理分页。

    除某些情况外,它在所有情况下都有效。在不起作用的情况下,我必须打开另一个窗口来查找信息并将其返回到主窗体。代码如下:

            // save current work
            updateDataTable();
    
            // Open a window and get new customer info
            // CurrentCustomer is returned from the opened window
    
            using (SqlConnection cx = new SqlConnection(GetConnectionString()))
            {
                DataRowView dataRow = (DataRowView)procBindingSource.Current;
                dataRow.BeginEdit();
                dataRow["CUSTOMER"] = CurrentCustomer; 
                dataRow.EndEdit();
                updateDataItems();
    
                SqlCommand cmd = new SqlCommand(
                    @" select acct_no from cust_processing where id = @id ", cx);
                cmd.Parameters.AddWithValue("@id", (int)dataRow["ID"]); 
                cx.Open();
                var results = cmd.ExecuteScalar();
                if (results != null)
                {
                    dataRow.BeginEdit();
                    dataRow["ACCT_NO"] = results.ToString(); 
                    dataRow.EndEdit();
                    updateDataItems();  <------   CONCURRENCY ERROR
    
                }  
            }
    

    我得到的错误是并发错误。我想我可能有多个版本的行?我想我是通过调用updateDataTable()来确保我在该行的最新版本上。我是唯一的用户,所以我知道我自己在制造问题。

    以下是我的更新方法,当我更改页面、保存并退出或要写入提交数据时调用:

    void  updateDataItems()
         {  
    
                this.procBindingSource.EndEdit();
                this.procTableAdapter.Update(xyzDataSet);
                xyzDataSet.AcceptChanges();
          }
    

    我尝试从不同的地方执行updatedataitems,比如在分配datarow[“acct_no”]=results.toString()之后,或者在分配它之前和之后。

    我几乎是在猜测和检查,所以任何想法,帮助和建议都将得到赞赏和+1。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Missy    7 年前

    好的——所以问题是我试图从程序中更新当前行,同时使用绑定导航器。他们在一起工作不正常。

    解决方案是在窗体设计器中向窗体中添加一个文本框,并将其设置为visible=false,然后将其绑定到acct_no。从其他窗体获得结果后,我只需将acct_no文本框的.text属性设置为新值,绑定导航器为我管理所有更新。正确地。

    txtAcct_No.text = results.ToString();