Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,

I created data layer class for typed dataset in vb.net 2010 and used my Presentation tier.
Everything works fine but one problem. When I make changes(adds, edits, deletes) to the instance(ie dsDis) of the data layer dataset in the presentation layer and
I call HasChanges method against dsDis, it returns false value.

I have include the entire code for both presentation and data layer.

I have spent several hours fix the problem but to no avail, please I need help.

Thanks in advance for your assistance.

The code are show below, first the presentation

Public Class frmAddDistrict
    Dim claDis As claDataLayer
    Dim dsDis As diaby_certDataSet
    Dim WithEvents newDistrict As BindingSource
    Private Shared anInstance As frmAddDistrict
    Public Shared ReadOnly Property instance As frmAddDistrict
        Get
            If anInstance Is Nothing Then
                anInstance = New frmAddDistrict
            End If
            Return anInstance
        End Get
    End Property
    Private Sub frmAddDistrict_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        anInstance = Nothing
    End Sub
    Private Sub frmAddDistrict_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        claDis = New claDataLayer
        dsDis = claDis.getDataSet
        Try
            newDistrict = New BindingSource
            With newDistrict
                .DataSource = dsDis
                .DataMember = "district"
            End With
            txtDistrictCode.DataBindings.Add("text", newDistrict, "d_code", True, DataSourceUpdateMode.OnValidation)
            txtDistrictName.DataBindings.Add("text", newDistrict, "name", True, DataSourceUpdateMode.OnValidation)
            txtLocation.DataBindings.Add("text", newDistrict, "location", True, DataSourceUpdateMode.OnValidation)
            cboRegion.DataBindings.Add("text", newDistrict, "region", True, DataSourceUpdateMode.OnValidation)

            Me.DataGridView1.DataSource = newDistrict

            newDistrict.AddNew()

        Catch ex As Exception

        End Try
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Try
            If btnAdd.Text = "&Save" Then
                If dsDis.HasChanges Then
                    newDistrict.EndEdit()
                    claDis.upDataSet(dsDis)
                    dsDis.AcceptChanges()
                    newDistrict.AddNew()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        newDistrict.CancelEdit()
        Me.btnAdd.Text = "&Add"
        Me.Text = "Add New Districts"
        Me.btnCancel.Enabled = False
    End Sub
    Private Sub newDistrict_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles newDistrict.AddingNew
        Me.btnAdd.Text = "&Save"
        Me.Text = Me.Text & " - Adding"
        Me.btnCancel.Enabled = True
        'Me.DataGridView1.
    End Sub
End Class


Data Layer:

Public Class claDataLayer
    'declare class level tableadapters
    Private taDistricts As diaby_certDataSetTableAdapters.districtTableAdapter
    Private taStaff As diaby_certDataSetTableAdapters.staffTableAdapter
    Private taSecurity As diaby_certDataSetTableAdapters.securityTableAdapter
    Private taYield As diaby_certDataSetTableAdapters.yieldsTableAdapter
    Private taFarms As diaby_certDataSetTableAdapters.farmsTableAdapter
    Private tafarmers As diaby_certDataSetTableAdapters.farmersTableAdapter

    'declare class level dataset
    Private dsDiabyDataSet As diaby_certDataSet

    'declare class level relation
    Private DistrictToStaff As DataRelation
    Private FarmersToFarms As DataRelation
    Private FarmsToYield As DataRelation
    Private StaffToFarmers As DataRelation
    Public Sub New()
        Try

            'instantiate tableadapters and dataset
            taDistricts = New diaby_certDataSetTableAdapters.districtTableAdapter
            taStaff = New diaby_certDataSetTableAdapters.staffTableAdapter
            taSecurity = New diaby_certDataSetTableAdapters.securityTableAdapter
            taYield = New diaby_certDataSetTableAdapters.yieldsTableAdapter
            taFarms = New diaby_certDataSetTableAdapters.farmsTableAdapter
            tafarmers = New diaby_certDataSetTableAdapters.farmersTableAdapter
            dsDiabyDataSet = New diaby_certDataSet

            'relation
            DistrictToStaff = dsDiabyDataSet.Relations!staff_district
            FarmersToFarms = dsDiabyDataSet.Relations!farms_farmers
            FarmsToYield = dsDiabyDataSet.Relations!yields_farms
            StaffToFarmers = dsDiabyDataSet.Relations!staff_farmers

            'fill dataset, fill parent first
            taDistricts.Fill(dsDiabyDataSet.district)
            taStaff.Fill(dsDiabyDataSet.staff)
            taSecurity.Fill(dsDiabyDataSet.security)
            taYield.Fill(dsDiabyDataSet.yields)
            tafarmers.Fill(dsDiabyDataSet.farmers)
            taFarms.Fill(dsDiabyDataSet.farms)

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Public Function getDataSet() As diaby_certDataSet
        Return dsDiabyDataSet
    End Function
    Public Sub upDataSet(ByVal ds As diaby_certDataSet)
        Try
            'save child deletes
            If ds.yields.GetChanges(DataRowState.Deleted) IsNot Nothing Then
                'get the changes(deleted)
                Dim YieldDeletedTable As diaby_certDataSet.yieldsDataTable = CType(ds.yields.GetChanges(DataRowState.Deleted), diaby_certDataSet.yieldsDataTable)
                'execute the changes
                taYield.Update(YieldDeletedTable)
            End If

            If ds.farms.GetChanges(DataRowState.Deleted) IsNot Nothing Then
                'get the changes(deleted)
                Dim FarmsDeletedTable As diaby_certDataSet.farmsDataTable = CType(ds.farms.GetChanges(DataRowState.Deleted), diaby_certDataSet.farmsDataTable)
                'execute the changes
                taFarms.Update(FarmsDeletedTable)
            End If

            If ds.farmers.GetChanges(DataRowState.Deleted) IsNot Nothing Then
                'get the changes(deleted)
                Dim FarmersDeletedTable As diaby_certDataSet.farmersDataTable = CType(ds.farmers.GetChanges(DataRowState.Deleted), diaby_certDataSet.farmersDataTable)
                'execute the changes
                tafarmers.Update(FarmersDeletedTable)
            End If

            If ds.staff.GetChanges(DataRowState.Deleted) IsNot Nothing Then
                'get the changes(deleted)
                Dim StaffDeletedTable As diaby_certDataSet.staffDataTable = CType(ds.staff.GetChanges(DataRowState.Deleted), diaby_certDataSet.staffDataTable)
                'execute the changes
                taStaff.Update(StaffDeletedTable)
            End If

            'update parent rows
            If ds.security.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)) IsNot Nothing Then
                taSecurity.Update(ds.security)
            End If
            If ds.district.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)) IsNot Nothing Then
                taDistricts.Update(ds.district)
            End If

            'child adds and edited
            If ds.yields.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)) IsNot Nothing Then
                'get the changes(deleted)
                Dim YielAddsEddit As diaby_certDataSet.yieldsDataTable = CType(ds.yields.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)), diaby_certDataSet.yieldsDataTable)
                'execute the changes
                taYield.Update(YielAddsEddit)
            End If

            If ds.farms.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)) IsNot Nothing Then
                'get the changes(deleted)
                Dim FarmsAddsEdit As diaby_certDataSet.farmsDataTable = CType(ds.farms.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)), diaby_certDataSet.farmsDataTable)
                'execute the changes
                taFarms.Update(FarmsAddsEdit)
            End If

            If ds.farmers.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)) IsNot Nothing Then
                'get the changes(deleted)
                Dim FarmersAddsEdit As diaby_certDataSet.farmersDataTable = CType(ds.farmers.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)), diaby_certDataSet.farmersDataTable)
                'execute the changes
                tafarmers.Update(FarmersAddsEdit)
            End If

            If ds.staff.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)) IsNot Nothing Then
                'get the changes(deleted)
                Dim StaffAddsEdits As diaby_certDataSet.staffDataTable = CType(ds.staff.GetChanges(CType(DataRowState.Added + DataRowState.Modified, DataRowState)), diaby_certDataSet.staffDataTable)
                'execute the changes
                taStaff.Update(StaffAddsEdits)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub


End Class
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900