1

we need to compare the content of 2 identical gridviews and extract rows that differ in a third gridview, is this doable?

i tried a lot but faced no luck, please help me.

at our office we take daily backup of ASP.net application ms access backend

for the next few days we need to evaluate the changes made to records in the database tables

at the end of each day i want to compare 2 access databases first database is the backup of yesterday and second database is the backup of today

i thought of the following algorithm, please read carefully and tell me how to proceed to compare the datatables / gridviews

i need to display th rows / cells containing the differences / updates / deleted data

comparing two ms access backend databases of an asp.net web application

Community
  • 1
  • 1
Mohamed Kamal
  • 2,377
  • 5
  • 32
  • 47

2 Answers2

1

my team and myseld figured it out

Imports System.Data
Imports System.Data.OleDb
Partial Class MoKoTrack
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim myDB = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|databaseName.mdb;Persist Security Info=True")

    Session("CurrentDB") = myDB


    myDB.open()
    Dim mytables = myDB.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, New Object() {})

    Dim CurrentTable As String

    For i = 1 To mytables.Rows.Count
        CurrentTable = mytables.Rows(i - 1).Item(2).ToString
        If CurrentTable.Contains("Backup") Then CompareTable(CurrentTable, mytables.Rows(i - 1).Item(3).ToString)
    Next i

    '        Dim myGrid As New GridView
    'myGrid.DataSource = mytables
    'myGrid.DataBind()
    'Me.Form.Controls.Add(myGrid)
    'myDB.Close()
End Sub

Sub CompareTable(ByVal BackupTableName As String, ByVal myPrimKey As String)

    Dim OriginalTable As New DataTable
    Dim BackupTable As New DataTable
    Dim ModificationsTable As New DataTable
    Dim AddedTable As New DataTable
    Dim DeletedTable As New DataTable

    Dim myDB = Session("CurrentDB")
    Dim FinalSQLString = "SELECT * FROM [" + BackupTableName + "]"
    Dim myDBCommand = New OleDbCommand(FinalSQLString, myDB)
    Dim myReader As IDataReader = myDBCommand.ExecuteReader()

    BackupTable.Load(myReader)

    Dim OriginalTableName = Left(BackupTableName, Len(BackupTableName) - 6)
    Dim FinalSQLString2 = "SELECT * FROM [" + OriginalTableName + "]"
    Dim myDBCommand2 = New OleDbCommand(FinalSQLString2, myDB)
    'Generate a temporary reader to get the number of cases
    Dim myReader2 As IDataReader = myDBCommand2.ExecuteReader()
    OriginalTable.Load(myReader2)

    Dim myPrimColumn(0) As DataColumn
    myPrimColumn(0) = OriginalTable.Columns(myPrimKey)
    OriginalTable.PrimaryKey = myPrimColumn
    Dim myPrimColumn2(0) As DataColumn
    myPrimColumn2(0) = BackupTable.Columns(myPrimKey)
    BackupTable.PrimaryKey = myPrimColumn2


    AddedTable = OriginalTable.Clone
    DeletedTable = OriginalTable.Clone
    ModificationsTable = OriginalTable.Clone
    ModificationsTable.PrimaryKey = Nothing

    Dim CurrentVal As String

    For i = 0 To OriginalTable.Rows.Count - 1
        CurrentVal = OriginalTable.Rows(i).Item(myPrimKey).ToString
        Dim foundRow As DataRow = BackupTable.Rows.Find(CurrentVal)
        If foundRow IsNot Nothing Then
            For t = 0 To OriginalTable.Columns.Count - 1
                If Not foundRow.Item(t).ToString = OriginalTable.Rows(i).Item(t).ToString Then
                    ModificationsTable.ImportRow(OriginalTable.Rows(i))
                    'ModificationsTable.Rows(ModificationsTable.Rows.Count - 1).Item(t) = ModificationsTable.Rows(ModificationsTable.Rows.Count - 1).Item(t) & "Modified"
                    ModificationsTable.ImportRow(foundRow)
                End If
            Next


        Else
            AddedTable.ImportRow(OriginalTable.Rows(i))
        End If

    Next

    For i = 0 To BackupTable.Rows.Count - 1
        CurrentVal = BackupTable.Rows(i).Item(myPrimKey).ToString
        Dim foundRow As DataRow = OriginalTable.Rows.Find(CurrentVal)
        If foundRow Is Nothing Then
            DeletedTable.ImportRow(OriginalTable.Rows(i))
        End If

    Next

    If AddedTable.Rows.Count > 0 Then
        Dim myLabel As New Label
        myLabel.Text = "<br/> The following records were added to table " & OriginalTableName & "<br/> <br/>"
        Me.form1.Controls.Add(myLabel)
        Dim myGrid As New GridView
        myGrid.DataSource = AddedTable
        myGrid.DataBind()
        Me.form1.Controls.Add(myGrid)
    End If

    If ModificationsTable.Rows.Count > 0 Then
        Dim myLabel As New Label
        myLabel.Text = "<br/> The following records were modified in table " & OriginalTableName & "<br/> <br/>"
        Me.form1.Controls.Add(myLabel)
        Dim myGrid As New GridView
        myGrid.DataSource = ModificationsTable
        myGrid.DataBind()
        Me.form1.Controls.Add(myGrid)
    End If

    If DeletedTable.Rows.Count > 0 Then
        Dim myLabel As New Label
        myLabel.Text = "<br/> The following records were deleted from table " & OriginalTableName & "<br/> <br/>"
        Me.form1.Controls.Add(myLabel)
        Dim myGrid As New GridView
        myGrid.DataSource = DeletedTable
        myGrid.DataBind()
        Me.form1.Controls.Add(myGrid)
    End If


End Sub

End Class
Mohamed Kamal
  • 2,377
  • 5
  • 32
  • 47
0

I usually compare it using LINQ

This will give you a head start

var SearchResults1 = from u in YourDB.YourUserTable1
                    orderby u.YourColumn
                    select u;

GridView1.DataSource = SearchResults1;
GridView1.DataBind();

var SearchResults2 = from u in YourDB.YourUserTable2
                        orderby u.YourColumn
                        select u;

GridView2.DataSource = SearchResults2;
GridView2.DataBind();

var SearchResults3 = from u1 in SearchResults2
                        where !(from u2 in SearchResults1
                                select u2.YourTablePrimaryKey).Contains(u1.YourTablePrimaryKey)
                        orderby u1.YourColumn
                        select u1;

GridView3.DataSource = SearchResults3;
GridView3.DataBind();

You can further elaborate SearchResults 3 if you want like comparing whats in SearchResults1 but not in SearchResults2 and vice versa

Raymund
  • 7,684
  • 5
  • 45
  • 78