Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to avoid duplicate entries in DataTable.

How to avoid duplicate entries in DataTable.

Scheduled Pinned Locked Moved ASP.NET
csharpcsssharepointwpfwcf
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VenkataRamana Gali
    wrote on last edited by
    #1

    Hi i am using .NET 1.1. I am populating some data in DataTable using StoredProcedure. DataTable dt = new DataTable(); DataRow dr; DataSet ds = GetDataFromSP(); for (int k=0;k{ dr = dt.NewRow(); dr[0] = int.Parse(ds.Tables[0].Rows[k]["ID"].ToString()); dr[1] = ds.Tables[0].Rows[k]["COL1"].ToString() dr[2] = ds.Tables[0].Rows[k]["COL2"].ToString() dt.Rows.Add(dr); } //Binding to Grid using dt. In this DataTable(dt) with same primary key multiple rows are returning by SP. i want to check DataTable weather with this primary key is there any dupliate entry. if entry exists in DataTable, avoid the adding into the dt. how to do this? any sounds appriciated. awaiting for response.

    regards GV Ramana

    G 1 Reply Last reply
    0
    • V VenkataRamana Gali

      Hi i am using .NET 1.1. I am populating some data in DataTable using StoredProcedure. DataTable dt = new DataTable(); DataRow dr; DataSet ds = GetDataFromSP(); for (int k=0;k{ dr = dt.NewRow(); dr[0] = int.Parse(ds.Tables[0].Rows[k]["ID"].ToString()); dr[1] = ds.Tables[0].Rows[k]["COL1"].ToString() dr[2] = ds.Tables[0].Rows[k]["COL2"].ToString() dt.Rows.Add(dr); } //Binding to Grid using dt. In this DataTable(dt) with same primary key multiple rows are returning by SP. i want to check DataTable weather with this primary key is there any dupliate entry. if entry exists in DataTable, avoid the adding into the dt. how to do this? any sounds appriciated. awaiting for response.

      regards GV Ramana

      G Offline
      G Offline
      goyal manish
      wrote on last edited by
      #2

      one thing which u can do is to get expected result from sp by using distinct keyword and if in yours case that is not possible. then take a string variable and put whenever you add the row in datatable also add that primay key in this string.(this will containf the comma separated primary keys which have added). and before adding the row to datatable check that whether this primary key is already there in string. //declare string as empty before for loop dr = dt.NewRow(); dr[0] = int.Parse(ds.Tables[0].Rows[k]["ID"].ToString()); //check that string contaims this primary key or not if( string does not contain this primary key) { dr[1] = ds.Tables[0].Rows[k]["COL1"].ToString() dr[2] = ds.Tables[0].Rows[k]["COL2"].ToString() dt.Rows.Add(dr); //add this primary key to string }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups