.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to Remove duplicate records from Datatable

Posted By:Rajshree       Posted Date: September 23, 2010    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  

If there are duplicate records in datatable we can easily show only distinct records without using any for loop. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class chkdup : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)


    protected void gdata()
        DataSet ds,ds1;
        DataTable tb = new DataTable();
        tb.Columns.Add(new DataColumn("EmpCode"));
        tb.Columns.Add(new DataColumn("EmpName"));

        for (int i = 0; i < 10; i++)

            DataRow dr = tb.NewRow();
            dr["EmpCode"] = i;
            dr["EmpName"] = "Rajshree" + i;
            DataRow dr1 = tb.NewRow();
            dr1["EmpCode"] = i;
            dr1["EmpName"] = "Rajshree" + i;

        ds = new DataSet();
        ds1 = new DataSet();
        DataTable tb2=new DataTable();

       tb2=ds.Tables["Employee"].DefaultView.ToTable(true, "EmpCode","EmpName");
       GridView1.DataSource = ds1.Tables[0];



Note: Take a gridview in .aspx page


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend