Compare and Delete duplicate Records from two DataTables in ASP.Net

Last Reply on Jun 04, 2018 04:06 AM By pandeyism

Posted on Jun 02, 2018 10:15 AM

i am creating online exam application now i want Compare two DataTables and Delete the Matched Records and bind the Remaining Data on GridView using c#
i have two tables
1.TestSeries
2.TestCompleted
in my TestSeries table i have the following records
1.Test Series 01
2.Test Series 02
3.Test Series 03
4.Test Series 04
5.Test Series 05
6.Test Series 06
7.Test Series 07
8.Test Series 08
9.Test Series 09
10.Test Series 10
and my TestCompleted table have the following Records 
1.Test Series 01
2.Test Series 02
3.Test Series 03
my expected output is like this 
1.Test Series 01
2.Test Series 02
3.Test Series 03
4.Test Series 04
5.Test Series 05
6.Test Series 06
7.Test Series 07

if anyone how to do this na suggest me 

Posted on Jun 04, 2018 04:06 AM

Hi paulrajmca,

Check this example. Now please take its reference and correct your code.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="UserId" HeaderText="UserId" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" />
    </Columns>
</asp:GridView>
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="UserId" HeaderText="UserId" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" />
    </Columns>
</asp:GridView>
<br />
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="UserId" HeaderText="UserId" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("UserId"), new DataColumn("UserName") });
        dt1.Rows.Add("01", "Test Series 01");
        dt1.Rows.Add("02", "Test Series 02");
        dt1.Rows.Add("03", "Test Series 03");
        dt1.Rows.Add("04", "Test Series 04");
        this.GridView1.DataSource = dt1;
        this.GridView1.DataBind();

        DataTable dt2 = new DataTable();
        dt2.Columns.AddRange(new DataColumn[2] { new DataColumn("UserId"), new DataColumn("UserName") });
        dt2.Rows.Add("01", "Test Series 01");
        dt2.Rows.Add("02", "Test Series 08");
        dt2.Rows.Add("03", "Test Series 03");
        this.GridView2.DataSource = dt2;
        this.GridView2.DataBind();

        DataTable dt3 = dt1.Copy();
        foreach (DataRow row1 in dt2.Rows)
        {
            bool isRecordExist = false;
            foreach (DataRow row2 in dt1.Rows)
            {
                if (row1["UserId"] == row2["UserId"] && row1["UserName"] == row2["UserName"])
                {
                    isRecordExist = true;
                }
            }
            if (!isRecordExist)
            {
                dt3.ImportRow(row1);
            }
        }

        this.GridView3.DataSource = dt3;
        this.GridView3.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt1 As DataTable = New DataTable()
        dt1.Columns.AddRange(New DataColumn(1) {New DataColumn("UserId"), New DataColumn("UserName")})
        dt1.Rows.Add("01", "Test Series 01")
        dt1.Rows.Add("02", "Test Series 02")
        dt1.Rows.Add("03", "Test Series 03")
        dt1.Rows.Add("04", "Test Series 04")
        Me.GridView1.DataSource = dt1
        Me.GridView1.DataBind()
        Dim dt2 As DataTable = New DataTable()
        dt2.Columns.AddRange(New DataColumn(1) {New DataColumn("UserId"), New DataColumn("UserName")})
        dt2.Rows.Add("01", "Test Series 01")
        dt2.Rows.Add("02", "Test Series 08")
        dt2.Rows.Add("03", "Test Series 03")
        Me.GridView2.DataSource = dt2
        Me.GridView2.DataBind()
        Dim dt3 As DataTable = dt1.Copy()

        For Each row1 As DataRow In dt2.Rows
            Dim isRecordExist As Boolean = False

            For Each row2 As DataRow In dt1.Rows

                If row1("UserId") = row2("UserId") AndAlso row1("UserName") = row2("UserName") Then
                    isRecordExist = True
                End If
            Next

            If Not isRecordExist Then
                dt3.ImportRow(row1)
            End If
        Next

        Me.GridView3.DataSource = dt3
        Me.GridView3.DataBind()
    End If
End Sub

Screenshot