none
DataSet.Merge - Merge typed DataSet to another and work with it.... RRS feed

  • Question

  • Hello

     

    I have a little question respective to the Merge Method of the DataSet.

     

    Please have look at the following code sample:

     

     

    1             SecondDataSet secondSet = new SecondDataSet();

     

          2     secondSet.SecondTable.AddSecondTableRow("TestColumn1", "TestColumn2");

     

                //.....

     

          3     MainDataSet mainSet = new MainDataSet();

    4      // **

          5     mainSet.Merge(secondSet);

              

                //.....

     

          6     SecondDataSet.SecondTableDataTable secondTable = mainSet.Tables["SecondTable"] as SecondDataSet.SecondTableDataTable;

     

          7     SecondDataSet.SecondTableRow secondTRow = secondTable[0];

     

          8     secondTRow.Column2 = "NewTestColumn2"; // <-- Causes 'Column 'Column2' does not belong to table SecondTable.

     

     

    I created two typed DataSet with one DataTable for each DataSet. [ MainDataSet(Table=MainTable1), SecondDataSet(Table=SecondTable) ]

     

    First I load some rows in the Business-Layer into the Table “SecondTable” of the “SecondDataSet”.
    On Client-Side e.g. I have a Form with an DataSet (MainDataSet) and merge the business results from “SecondDataSet” to

    the “MainDataSet”.

     

    After this, I assign the MainDataSet as a data source of a UserControl.

     

    Now in the UserControl I will edit the rows in the merged “SecondTable”. For this I create a typed DataTable from the Tables of the MainSet. (Line 6)

    On assign a value to the Column2 I get the Error 'Column 'Column2' does not belong to table SecondTable’. (Line 8)

     

    This error is caused because the Table-Property of the Column2-Column (private member of the typed DataTable SecondTable) is null

    after the merging operation (mainSet.Merge(secondSet);).

     

    My question is: Why isn’t set the Table-Property of the typed DataColumn after merging (Line 5)?

     

    If I add the Table ‘SecondTable’ to the MainDataSet with ‘’mainSet.Tables.Add(new SecondDataSet.SecondTableDataTable());” (Line 4)

    before merging, the code works, but I can’t add the “unknown” tables.

     

    I hope you can help me to resolve this problem. (This code is created with .NET 3.5)

     

    With kind regards

     

     

    Thorsten Steinberg

    Wednesday, December 21, 2011 10:11 AM

All replies

  • Hi Thorsten,

    This will not work unless your SecondDataSet directly derives from MainDataSet. It should be obvious that, despite table name equality ("SecondTable"), the types of the two tables involved are totally distinct. They have different type identities. So, if no inheritance is involved, and there also is no type converter to convert between the two types, there is no way of converting MainDataSet.SecondTableDataTable into (as) SecondDataSet.SecondTableDataTable.

    Just to show you the difference. The following code however works O.K.:

    // Test
    SecondNorthwindDataSet secondSet = new SecondNorthwindDataSet();
    secondSet.Customers.AddCustomersRow("etc", "etc", "etc", "etc", "etc", "etc", null, "etc", "etc", null, null, DateTime.Now, DateTime.Now);
    NorthwindDataSet mainSet = new NorthwindDataSet();
    mainSet.Merge(secondSet);
    SecondNorthwindDataSet.CustomersDataTable secondTable = mainSet.Tables["Customers"] as SecondNorthwindDataSet.CustomersDataTable;
    SecondNorthwindDataSet.CustomersRow secondRow = secondTable[0];
    secondRow.City = "bogus";
    
    // Derived class
    class SecondNorthwindDataSet : NorthwindDataSet {  }
    


    You are working with typed data sets. There is no place for unknown structure information with typed datasets.
    (Please correct me, if I got you wrong.)

    Marcel

    Wednesday, December 21, 2011 2:57 PM
  • Marcel, thanks for reply ...

    i don't have something like this you wrote: MainDataSet.SecondTableDataTable into (as) SecondDataSet.SecondTableDataTable..

    The MainDataSet only have a typed DataTable called MainTable1.
    The SecondDataSet only have a typed DataTable called SecondTable.

    (See image below!)

    The type casting SecondDataSet.SecondTableDataTable secondTable = mainSet.Tables["SecondTable"] as SecondDataSet.SecondTableDataTable; works correct!

    But the private DataColumn members of the casted datatable don't have
    set the table property. And thats why i can't work further with typed rows.

    Hello

     

    Wednesday, December 21, 2011 4:10 PM
  • Thorsten,

    Please rename "MainTable1" to "SecondTable".
    Then do your referencing like this:

    SecondDataSet secondSet = new SecondDataSet();
    secondSet.SecondTable.AddSecondTableRow("TestColumn1", "TestColumn2");
    MainDataSet mainSet = new MainDataSet();
    mainSet.Merge(secondSet); 
    MainDataSet.SecondTableRow secondTRow = mainSet.SecondTable[0];
    secondTRow.Column2 = "NewTestColumn2"; // <-- Caused 'Column 'Column2' does not belong to table SecondTable.
    

    The exception in your original code was thrown because of a reference mismatch in DataRow.CheckColumn().
    The code there looks like this:

    // DataRow.cs
    private void CheckColumn(DataColumn column) {
      if (column == null) {
        throw ExceptionBuilder.ArgumentNull("column");
      }
      if (column.Table != this._table) {
        throw ExceptionBuilder.ColumnNotInTheTable(column.ColumnName, this._table.TableName);
      }
    }


    Marcel


    • Edited by Marcel Roma Wednesday, December 21, 2011 7:55 PM
    Wednesday, December 21, 2011 7:29 PM
  • Can I ask a stupid question? Why do you want to merge the SecondDataSet with the MainDataSet? Can you not just use the SecondDataSet instead? What are you using the two DataSets/DataTables for?
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, December 22, 2011 5:39 AM
  • Hi Bonnie,

    I presumed that Thorsten was trying to merge table data from a middle tier with table data already existing client-sides. In other words, if MainDataSet.[Table] initially contained say 2 rows and the schematically identical SecondDataSet.[Table] had 3 new rows, then after DataSet.Merge() the table in MainDataSet would contain 5 rows, etc.

    MainDataSet on the client could contain a lot of different tables not available in SecondDataSet, or even contain extra columns in the table targeted by Merge(). But I'm sure Thorsten will clarify it all himself.

    Marcel


    • Edited by Marcel Roma Thursday, December 22, 2011 8:42 AM
    Thursday, December 22, 2011 8:26 AM
  • Hi Marcel,

    I got the idea that the two DataSets weren't schematically the same. IOW, MainDataSet did NOT contain a DataTable called SecondTable. As you know, the tables will merge into the intstance of MainDataSet just fine, but there won't be any typed data in any tables that weren't in the MainDataSet.

    We'll definitely have to wait for clarification from Thorsten.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, December 22, 2011 3:29 PM
  • Hi Thorsten,

    Welcome to MSDN Forum.

    Have you solved the issue? I look forward hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 26, 2011 2:09 AM
    Moderator