C# .NET - how to merge the two dataset without duplicates
Asked By thirumaran d
10-Sep-10 07:15 AM
hai freinds
I am having two datasets like this:
ds
TBLKEY EMPKEY EMPNAME
1 101 RAJA
2 105 POOJA
3 106 THRIU
ds2
TBLKEY EMPKEY EMPNAME
1 101 RAJA
2 105 POOJA
3 106 THRIU
4 109 karthik
5 110 hari
i shoud get a output in ds like this here there is no primarykey in any column how to merge this
TBLKEY EMPKEY EMPNAME
1 101 RAJA
2 105 POOJA
3 106 THRIU
4 109 karthik
5 110 hari
Sagar P replied to thirumaran d

I think you have to write your own logic for this....
See this function;
public static DataSet MergeResultSets()
{
DataSet ds = null;
int checkCount = 0;
string rowPosition = "";
try {
ds = DAL.DataAcess.Sample(param1, param2);
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) {
for (int j = 0; j <= ds.Tables[1].Rows.Count - 1; j++) {
if (Equals(ds.Tables[0].Rows[i].ItemArray().Count, ds.Tables[1].Rows[j].ItemArray().Count)) {
for (int k = 0; k <= ds.Tables[0].Rows[i].ItemArray().Count - 1; k++) {
if (Equals(ds.Tables[0].Rows[i].ItemArray[k], ds.Tables[1].Rows[j].ItemArray[k])) {
checkCount = checkCount + 1;
}
if (checkCount == ds.Tables[1].Rows[j].ItemArray().Count) {
rowPosition = rowPosition + j.ToString() + ",";
checkCount = 0;
}
}
checkCount = 0;
} else {
//Throw an exception or delete the row here itself
}
}
}
Array sa = Strings.Split(rowPosition.TrimEnd(","), ",");
for (int rp = 0; rp <= sa.Length - 1; rp++) {
ds.Tables[1].Rows[sa(rp)].Delete();
}
ds.Tables[1].AcceptChanges();
ds.Tables[0].Merge(ds.Tables[1]);
return ds;
// this has the two datatables merged as one dataset / datatable
// without duplicate rows.
} catch (Exception ex) {
return null;
}
}
http://www.codeproject.com/KB/grid/Merging_two_DataSets.aspx
[ Kirtan ] replied to thirumaran d
Assume that you Have two DataTables ds1,ds2 Respectively ..
and both contain 1 tables as you have mentioned in question
you can Merge Table 2 Into Table1 Like below
DataTable dt1 = ds1.Tables[0];
DataTable dt2 = ds2.Tables[0];
dt1.Merge(dt2);
/* dt1 Contain Combined Data Nows now without duplication */
thirumaran d replied to [ Kirtan ]
i f i use u r coding it is cuming like dupilcates records not deleted
| SerialNo |
TblKey |
EmpKey |
EmpName |
| 1 |
1 |
101 |
RAJA |
| 2 |
2 |
105 |
POOJA |
| 3 |
3 |
106 |
THRIU |
| 4 |
1 |
101 |
Raja |
| 5 |
2 |
105 |
POOJA |
| 6 |
3 |
107 |
Karthik |
| 7 |
4 |
108 |
Hari |
| |
|
|
|
[ Kirtan ] replied to thirumaran d
Duplicates are Because of your ID are Mismatching in Both Tables that makes both rows Unique thats why its coming in duplication
ID Name
1 RAJA
ID Name
2 RAJA
both are considered to be different rows
if both Contain Same ID and Same Name then its going to Removed from duplication.

Updating DataRow throws Exception 0xC0000005 .NET Framework Hi guys, I'm facing a very puzzling problem and thought maybe One of the code bits is a button to consolidate data, merging rows in a DataTable: [CODE] private void btnConsolidar_Click(object sender, System.EventArgs e) { for( int j = 0; j < dataTable.Rows.Count; j++ ) { DataRow row1 = dataTable.Rows[j]; String ean = (String)row1["ean"]; String cod = (String)( ( row1["codigo"] = = null ) ? "" : row1["codigo int idx; for( idx = j+1; idx < dataTable.Rows.Count; idx++ ) { DataRow row2 = dataTable.Rows[idx]; if( ((String)row2["ean"]) = = ean | | (cod ! = "" && ((String)row2["codigo"]) = = cod )) { long q1 = long if( bConferencia ) { if( (string)row1["quant1"] = = "0" ) { string q = (string)row2["quant1"]; / / row1["quant1"] = q; dataTable.Rows[j]["quant1"] = q; / / CRASH! } if( (string)row1["quant2"] = = "0" ) / / row1["quant2"] = (string)row2["quant2"]; dataTable.Rows[j]["quant2"] = (string)row2["quant2"]; } row1["quant"] = q1+q2; / / remove a fileira copiada dataTable
Index on DataTable ? .NET Framework Can I create an index on a DataTable ? On the following DataTable, I would like to create index on "Price", can I do that ? Thank you. Dim DT As New DataTable DT.Columns.Add("Price", GetType(System.Int32)) DT.Columns.Add("ID", GetType(System.Int32)) DT 1) = 1231 r.Item(2) = 250 r.Item(3) = 1 DT.Rows.Add(r) DT.AcceptChanges() VB.NET Discussions DataTable (1) DataRow (1) NewRow (1) AcceptChanges (1) Int32 (1) Database (1) fniles, No, I don't believe you can add an index to a datatable. Keep in mind that datasets / datatables are not the same as an in-memory sql
check unique value before adding to DataTable Hi friends i have one datatable and i want i must contain unique value, i set: dt.Columns[0].Unique = true; now value added on datatable form textbox, i want to prevent add duplicate value in datatable without loop, so that unique value must be added in datatable how can i do it, help me Thanks For this use this code- string s AnyTable" ].Rows.Find(s); if (foundRow ! = null ) { MessageBox.Show("Already Exist")); } else { / / Add Value to DataTable } Try this code and let me know, While looking for something similar, only, specifically for .net 2.0 Im assuming the OP was looking for distinct while using DataTable.Select(). (Select() doesn't support distinct) So here is the code from the link http support.microsoft.com / default.aspx?scid = kb;en-us;326176#1 class DataTableHelper { public DataTable SelectDistinct( string TableName, DataTable SourceTable, string FieldName) { DataTable dt = new DataTable(TableName); dt.Columns.Add
A DataTable-bound DataGridView throws a low-level Exception when columns are sorted, removed and re-added to the underlying DataTable. .NET Framework Hi, I am experiencing this issue when I am working on creating a posting this in the wrong newsgroup please point me to the right newsgroup. Issue: A DataTable-bound DataGridView throws a low-level Exception when columns are sorted, removed and re-added to the underlying DataTable. Steps to replicate the issue: 1. Create a DataTable. 2. Create a DataGridView and bind it to the DataTable. 3. Populate the datatable with data 4. SORT a column of the DataView (Exception is
common columns in them with c# thanks in advance Muthu HI try this public static DataTable CompareTwoDataTable( DataTable dt1, DataTable dt2) { dt1.Merge(dt2); DataTable d3 = dt2.GetChanges(); return d3; } (or) public DataTable DataTableJoiner(DataTable dt1, DataTable dt2) { using (DataTable targetTable = dt1.Clone()) { var dt2Query = dt2.Columns.OfType<DataColumn> ().Select(dc = > new DataColumn(dc.ColumnName