Click to See Complete Forum and Search --> : Problem Mering DataSets


ashram
September 5th, 2007, 03:02 PM
Here's an example program:

DataSet ds = new DataSet();
DataSet ds2 = new DataSet();

DataTable dt = new DataTable("Table1");
DataColumn dc = new DataColumn("Col1", typeof(int));
dt.Columns.Add(dc);
DataTable dt2 = new DataTable("Table1");
DataColumn dc2 = new DataColumn("col1", typeof(int));
dt2.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("Col2", typeof(int));
dt.Columns.Add(dc3);
ds.Tables.Add(dt);
dt.PrimaryKey = new DataColumn[] { dc };
ds2.Tables.Add(dt2);
dt2.PrimaryKey = new DataColumn[] { dc2 };
DataColumn dc4 = new DataColumn("col2", typeof(int));
dt2.Columns.Add(dc4);

DataRow dr = ds.Tables[0].NewRow();
dr[0] = 1;
dr[1] = 2;
ds.Tables[0].Rows.Add(dr);

dr = ds2.Tables[0].NewRow();
dr[0] = 12;
dr[1] = 22;
ds2.Tables[0].Rows.Add(dr);

foreach (DataTable tdt in ds.Tables)
{
foreach (DataColumn tdc in tdt.Columns)
{
tdc.ColumnName = tdc.ColumnName.ToLower();
}
}
ds.Merge(ds2, false, MissingSchemaAction.AddWithKey);

This causes a key violation because it adds the columns in ds2 as new columns, even though the column names and ordinals match.

If you did the reverse, change ds2 to mtach ds column name, the merge works as expected.

Anyway around this? I would like to be able to change the target dataset (ds), instead of the source (ds2).

Thanks.

cjard
September 11th, 2007, 04:38 AM
Youre right, ive investigated and found the columns to be identical, even the hashcode, when their name was changed. I cant see a reason for the behaviour you observe, but I'd say it's a bug.

You can avoid it, and have this work as you wish, by ensuring the column names are correct from the start. When I changed the strings in the column constructors to all lowercase, it worked ok..