CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2007
    Posts
    1

    Thumbs down Problem Mering DataSets

    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.

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Problem Mering DataSets

    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..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured