A
Alexander Fowler
Hello and thanks for taking a minute. I am just getting into ado.net and
still at the "experiment with wizard generated code" phase of self-teaching,
so please bear with any stupid mistakes.
I am trying to write a simple contact manager with an interface similar to
Act's opening contact screen. I've got enough down to pull data from various
data adapters into my datasets and bind to the form. However, I am having a
real problem with managing concurrency. I would like my application to only
write those records that are either additions or have updates. The following
code partially works in that it writes changes made to the dataset. However,
even when I do not update any data, the dataset always records at least one
change (as witnessed by the firing of the message box indicating how many
records were updated) and overwrites. Here is what happened:
1. I populated datasets in my vb.net build
2. I used SQL Enterprise Manager to change values in several rows of the
database table in question (simulating another user's work)
3. I edited one record in the vb.net build (simulating this user's work)
4. Ran update procedure in the vb.net build, which promptly updated the
entire table back to the data from my dataset (this user's work wiping out
another user's work).
What I need to have happen is that at step 4, only those records that were
edited in the vb.net build would be overwritten, not the entire dataset
(this user's work not affecting another user's work)
Below are two snippets of code that I used to populate the datasets and to
update the database, respectively. If somebody could point me in the right
direction, I would be very grateful. Thank you very much.
<!--- CODE TO POPULATE DATA STARTS HERE
daContacts.Fill(dsContacts1, "tblContacts")
daContacts_PaymentMethods.Fill(dsContacts1, "tblContacts_PaymentMethods")
daCBOCompanyNames.Fill(dsComboData1, "qryCBO_CompanyNames")
daCBOContactStatus.Fill(dsComboData1, "qryCBO_ContactStatus")
daCBOEmployees.Fill(dsComboData1, "qryCBO_Employees")
daCBOContactGroups.Fill(dsComboData1, "qryCBO_ContactGroups")
daCBOSalesReps.Fill(dsComboData1, "qryCBO_EmployeesSales")
Dim dtInsertName As DataTable = dsComboData1.Tables("qryCBO_CompanyNames")
Dim drInsertName As DataRow = dtInsertName.NewRow()
drInsertName("CompanyName") = "Individual"
dtInsertName.Rows.Add(drInsertName)
dsComboData1.AcceptChanges()
Me.PositionChange()
CODE ENDS HERE ---!>
<!--- CODE FOR UPDATING DATABASE STARTS HERE
Public Sub UpdateDB()
Me.BindingContext(dsContacts1, "tblContacts").EndCurrentEdit()
Try
Dim drAdded As DataRow() =
dsContacts1.Tables("tblContacts").Select(Nothing, Nothing,
DataViewRowState.Added)
Dim drModified As DataRow() =
dsContacts1.Tables("tblContacts").Select(Nothing, Nothing,
DataViewRowState.ModifiedCurrent)
connContacts.Open()
If Not (drAdded.Length = 0) Then
daContacts.Update(drAdded)
MessageBox.Show(drAdded.Length.ToString & " records were
added.", "Database Confirmation", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("No Records to Add")
End If
If Not (drModified.Length = 0) Then
daContacts.Update(drModified)
MessageBox.Show(drModified.Length.ToString & " records were
changed.", "Database Confirmation", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("No Records to Modify")
End If
Catch eInsertException As Exception
MessageBox.Show(eInsertException.Message)
Throw eInsertException
Finally
connContacts.Close()
End Try
End Sub
CODE ENDS HERE ---!>
still at the "experiment with wizard generated code" phase of self-teaching,
so please bear with any stupid mistakes.
I am trying to write a simple contact manager with an interface similar to
Act's opening contact screen. I've got enough down to pull data from various
data adapters into my datasets and bind to the form. However, I am having a
real problem with managing concurrency. I would like my application to only
write those records that are either additions or have updates. The following
code partially works in that it writes changes made to the dataset. However,
even when I do not update any data, the dataset always records at least one
change (as witnessed by the firing of the message box indicating how many
records were updated) and overwrites. Here is what happened:
1. I populated datasets in my vb.net build
2. I used SQL Enterprise Manager to change values in several rows of the
database table in question (simulating another user's work)
3. I edited one record in the vb.net build (simulating this user's work)
4. Ran update procedure in the vb.net build, which promptly updated the
entire table back to the data from my dataset (this user's work wiping out
another user's work).
What I need to have happen is that at step 4, only those records that were
edited in the vb.net build would be overwritten, not the entire dataset
(this user's work not affecting another user's work)
Below are two snippets of code that I used to populate the datasets and to
update the database, respectively. If somebody could point me in the right
direction, I would be very grateful. Thank you very much.
<!--- CODE TO POPULATE DATA STARTS HERE
daContacts.Fill(dsContacts1, "tblContacts")
daContacts_PaymentMethods.Fill(dsContacts1, "tblContacts_PaymentMethods")
daCBOCompanyNames.Fill(dsComboData1, "qryCBO_CompanyNames")
daCBOContactStatus.Fill(dsComboData1, "qryCBO_ContactStatus")
daCBOEmployees.Fill(dsComboData1, "qryCBO_Employees")
daCBOContactGroups.Fill(dsComboData1, "qryCBO_ContactGroups")
daCBOSalesReps.Fill(dsComboData1, "qryCBO_EmployeesSales")
Dim dtInsertName As DataTable = dsComboData1.Tables("qryCBO_CompanyNames")
Dim drInsertName As DataRow = dtInsertName.NewRow()
drInsertName("CompanyName") = "Individual"
dtInsertName.Rows.Add(drInsertName)
dsComboData1.AcceptChanges()
Me.PositionChange()
CODE ENDS HERE ---!>
<!--- CODE FOR UPDATING DATABASE STARTS HERE
Public Sub UpdateDB()
Me.BindingContext(dsContacts1, "tblContacts").EndCurrentEdit()
Try
Dim drAdded As DataRow() =
dsContacts1.Tables("tblContacts").Select(Nothing, Nothing,
DataViewRowState.Added)
Dim drModified As DataRow() =
dsContacts1.Tables("tblContacts").Select(Nothing, Nothing,
DataViewRowState.ModifiedCurrent)
connContacts.Open()
If Not (drAdded.Length = 0) Then
daContacts.Update(drAdded)
MessageBox.Show(drAdded.Length.ToString & " records were
added.", "Database Confirmation", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("No Records to Add")
End If
If Not (drModified.Length = 0) Then
daContacts.Update(drModified)
MessageBox.Show(drModified.Length.ToString & " records were
changed.", "Database Confirmation", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("No Records to Modify")
End If
Catch eInsertException As Exception
MessageBox.Show(eInsertException.Message)
Throw eInsertException
Finally
connContacts.Close()
End Try
End Sub
CODE ENDS HERE ---!>