Remove duplicate records from a table

In this article I am explaining about how to Remove duplicate records from a table

This code sample shows how to identify and remove duplicate records from a dataset/datatable.

Following Code will take inputs datatable and column name wherein we need to find duplicates items and remove.

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();

//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dTable.Rows)
if (hTable.Contains(drow[colName]))
hTable.Add(drow[colName], string.Empty);

//Removing a list of duplicate items from datatable.
foreach (DataRow dRow in duplicateList)

//Datatable which contains unique records will be return as output.
return dTable;

Example showing how to use above method:

protected void Button1_Click(object sender, EventArgs e)
string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strConn);

SqlDataAdapter da = new SqlDataAdapter("select * from emp", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Emp");

// Filling a employee table
DataTable dt = ds.Tables["Emp"];
dt = RemoveDuplicateRows(dt, "empname");

GridView1.DataSource = ds.Tables["Emp"].DefaultView;


Author: Varma Suresh14 Oct 2009 Member Level: Gold   Points : 1

hi,It is good ,Is their any pre defined method to do.


Guest Author: vikul js27 Jun 2012

Thanks ..............

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: