How to avoid duplicate entries in DataTable.
-
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
-
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
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 }