Please how to handling Concurrency Violation Using BindingSource?

witecloner

Well-known member
Joined
Feb 12, 2009
Messages
45
Programming Experience
Beginner
Dear all, I have a problem when using binding source. i know that binding source could not handling the concurrency violation problem. Some of them told me to use datarowstate. but until now, i don't know how to code it. Could anybody help me?

Ok here is my code :
VB.NET:
'BW is BackgroundWorker Control
'BS is BindingSource Control
'DGV is DataGridView Control
'SqlConnection1 is SqlConnection Control , and so on...

Private Sub Form1_Load ( ... )
  BW.WorkerSupportCancellation = True
  BW.RunWorkerAsync()
End Sub

Private Sub BW_DoWork ( ... )
  LoadData()
End Sub

Private Sub BW_RunWorkerCompleted ( ... )
  DGV.DataSource = BS
  FormatStyle()
  BW,CancelAsync()
End Sub

Private Sub LoadData()
  try
     if SqlConnection1.State = ConnectionState.Closed Then _
         SqlConnection1.Open()
     DataSet1 = New DataSet
     DataSet1.Tables.Add("Customer")
     DataSet1.EnforceConstraints = False
     DataSet1.Table("Customer").BeginLoadData()
     SqlDataAdapter3.Fill(Dataset1.Table("Customer"))
     Dataset1.Table("Customer").EndLoad()
     BS.DataSource = DataSet1
     BS.DataMember = "Customer"
  Catch 
     MessageBox.Show(Ex.toString())
  Finally
     IF SqlConnection1 IsNot Nothing Then _
          SqlConnection1.Close()
  End Try
End Sub

Private Sub FormatStyle()
  With  DGV
     .Columns("CustID").HeaderText = "Customer"
     .Columns("CustName").HeaderText = "Name"
     .Columns("CustAddr").HeaderText = "Address"
     .Columns("CustCity").HeaderText = "City"
  End With
End Sub

Ok, at Form1 i place two button for call the other form (Call it form2). Form2 is used for handling insert or update commands. Here above the code for form2.

VB.NET:
Private Sub Form2_Load ( ... )
  If Form1.Mode  = "Insert" Then 
     'Clear TextBox
  ElseIf Form1.Mode = "Update" Then
     BindData()
  End If
End Sub

Private Sub BindData()
   'This Routine Just Binding BS to TextBox
End Sub

Private Sub ButtonSave ( ... )
  If Form1.Mode = "Insert" Then 
    'Insert New Row to Dataset and Update it...
    'Until here all code is work finely
  ElseIf Form1.Mode = "Update" Then
     Modify()
  End If
End Sub

Private Sub Modify()
  dim DT as DataTable() = New datatable
  Try
     Form1.BS.EndEdit()
     DT = CType(Form1.DataSet1.Table("Customer").GetChanges(Data.DataRowState,Modified, DataTable)

     If DT IsNot Nothing Then
        Form1.SqlDataAdapter1.Update(DT)
     End If
     Form1.DataSet1.AcceptChanges()
     MessageBox.Show("Success")
  Catch ex As Exception
     Form1.DataSet1.RejectChanges()
     MessageBox.Show("Failed")
  Finally
     If DT IsNot Nothing Then _
         DT.Dispose
  End Try
End Sub

Okay now i run that project in debug mode. I try to modify record. It's work, the record is modified finely.

Then i try run this project again, but now i run twice at the same time. First using debug mode (call it User 1), and second using Apps.Exe in Debug Folder (call it User 2).
I try to modify the record using user1 Form(at this time i don't save this record first just left it), then i modified the same record again (like user1 record) using user2 Form, for this time(using user2 form) i press button save for updating row, after that i press save button too for user1 Form. User1 and User2 can modify that record without any problem but just user2 record was accepted changes, and no for user1.

Please could anybody tell me how can i make user1 could not update row and show concurrency violation?

Thank's
 
Thank's i have founded the solution with your recommendation brother...

here above the code where i modified :

VB.NET:
Private Sub Modify()
  dim DT as DataTable() = New datatable
  Try
     Form1.BS.EndEdit()
     DT = CType(Form1.DataSet1.Table("Customer").GetChanges(Data.DataRowState,Modified, DataTable)

     If DT IsNot Nothing Then
        Form1.SqlDataAdapter1.Update(DT)
        Form1.DataSet1.AcceptChanges()
        MessageBox.Show("Success")
     Else
        Form1.DataSet1.RejectChanges()
        MessageBox.Show("Failed")
     End If
  Catch ex As Exception
     Form1.DataSet1.RejectChanges()
     MessageBox.Show("Failed")
  Finally
     If DT IsNot Nothing Then _
         DT.Dispose
  End Try
End Sub

Thank's
 
Back
Top