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


Top 5 Contributors of the Month
david stephan

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)
        {
            gdata();
        }


    }

    protected void gdata()
    {
        DataSet ds,ds1;
        DataTable tb = new DataTable();
        tb.TableName="Employee";
        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;
           
            tb.Rows.Add(dr);
            DataRow dr1 = tb.NewRow();
            dr1["EmpCode"] = i;
            dr1["EmpName"] = "Rajshree" + i;
            tb.Rows.Add(dr1);
        }

        
        ds = new DataSet();
        ds1 = new DataSet();
        ds.Tables.Add(tb);
        DataTable tb2=new DataTable();

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




    }


}



Note: Take a gridview in .aspx page

     

Further Readings:

Responses

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