[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Advice on determining which table to update using Dataset.HasChanges property

Posted on 2008-02-11
3
Medium Priority
?
144 Views
Last Modified: 2013-11-07
Hi,
I have a .Net 2.0 WinForms application which displays and allows users to edit data concerning corporate contacts. It has one dataset with many tables. Users can select a contact and make changes to the tables' data, then when they move to another record or close the form, my code examines the dataset.haschanges property. If it's true, I need to see which table has been changed in order to determine which update stored proc to run. My code for how to do this is attached, and my question is does this look OK to you, does it look like the right way to do it? And if not can anyone suggest an alternative (I know it may be that there's a whole other approach which I'm not aware of).
Thanks,
Louise

Private Sub GetTableName()
 
        Try
 
            'variables for table names
            Dim sContactsTable As String = "SpContacts1"
            Dim sOrgTable As String = "spOrgs1"
            Dim sOwnerTable As String = "spContactOwner"
            Dim sRelationshipsTable As String = "spContactcorporatePeople"
 
            'dataset, tables and rows
            dsChanges = frmData.Ds1.GetChanges(DataRowState.Modified Or DataRowState.Deleted)
 
            Dim modItems As New ArrayList
            Dim sMod As String = ""
 
            Dim delItems As New ArrayList
            Dim sDel As String = ""
 
            For Each t In dsChanges.Tables
                For Each r In t.Rows
                    If r.RowState = DataRowState.Modified Then modItems.Add(t.TableName)
                Next r
            Next t
 
            For i As Integer = 0 To modItems.Count - 1
                sMod += modItems.Item(i) & " "
            Next
 
            '2. run stored procedures
 
            If sMod.Contains(sContactsTable) Then
                sConfirm = "Please confirm changes to " & cRow("Contact")
                If MessageBox.Show(sConfirm, "Please confirm your changes", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
                    UpdateContact()
                Else : frmData.Ds1.RejectChanges()
                End If
            End If
 
            If sMod.Contains(sOrgTable) Then
                sConfirm = "Please confirm changes to " & _
                oRow("OrganisationName", DataRowVersion.Original) & vbCrLf & _
                "This change will affect all contacts at " _
                & oRow("OrganisationName", DataRowVersion.Original) & _
                ". Please confirm that you wish to continue."
                If MessageBox.Show(sConfirm, "Please confirm change", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
                    'UpdateOrg()
                Else : frmData.Ds1.RejectChanges()
                End If
            End If
 
            If sMod.Contains(sRelationshipsTable) Then
                sConfirm = "Please confirm changes to corporate relationships for " & cRow("Contact")
                If MessageBox.Show(sConfirm, "Please confirm", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then UpdatecorporatePeople()
            End If
 
        Catch ex As Exception
            LogException("Exception", ex.ToString, "frmContacts GetTableName")
            MessageBox.Show(sExText, sExCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
 
        End Try
 
    End Sub

Open in new window

0
Comment
Question by:louise001
3 Comments
 
LVL 11

Accepted Solution

by:
PlatoConsultant earned 1000 total points
ID: 20872046

 this will give u all changes only changed rows,,, if u iterate through dschanges and find tables whose row count is greater then 0 then ur problem will be solved insa.

   dsChanges = frmData.Ds1.GetChanges()


   For Each t In dsChanges.Tables
                For Each r In t.Rows.COUNT > 0
                    modItems.Add(t.TableName)
                Next r
            Next t
0
 
LVL 41

Expert Comment

by:graye
ID: 21145202
Your code looks fine... it's elegant and efficient
0
 

Author Closing Comment

by:louise001
ID: 31429862
Thanks very much
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question