none
How to consolidate code from two typed datasets

    Question

  • I have two typed datasets that have the same fields, but different data. One of them represents history, the other work (current). In multiple places in my code, I look for data in one of these datasets; if it's there, I create an object or otherwise manipulate the data, if it's not, I do the exact same thing, only using the other dataset.

    I know there is an easier way to go about this, but I'm not looking in the right places or thinking of the problem in the right way to arrive at the solution. Here's an example to help clarify:

    public static Invoice GetInvoiceData(string InvoiceNumber)
    {
         taInvoiceHistory = new InvoiceHistoryTableAdapter();
         InvoiceHistoryDataTable dtInvoiceHistory = new InvoiceHistoryDataTable();
         dtInvoiceHistory = taInvoiceHistory.GetDataByInvoiceNumber(InvoiceNumber);
         if (dtInvoiceHistory.Rows.Count==0)
      { taInvoiceWork = new InvoiceWorkTableAdapter(); InvoiceWorkDataTable dtInvoiceWork = new InvoiceWorkDataTable(); dtInvoiceWork = taInvoiceWork.GetDataByInvoiceNumber(InvoiceNumber); //manipulate the data in dtInvoiceWork in some way } else { //manipulate the data in dtInvoiceHistory //exactly the same way as above } }
    I've tried casting the tables to a more generic type, but can't seem to get that to work right, and I'm not even sure if that's the direction I should be looking at. Any help anyone can provide is greatly appreciated.

     
    Tuesday, December 29, 2009 9:54 PM

Answers

  • You might want to try the DataSet.Merge() method. Maybe something along these lines:
    public static Invoice GetInvoiceData(string InvoiceNumber)
    {
         taInvoiceHistory = new InvoiceHistoryTableAdapter();
         InvoiceHistoryDataTable dtInvoiceHistory = new InvoiceHistoryDataTable();
         dtInvoiceHistory = taInvoiceHistory.GetDataByInvoiceNumber(InvoiceNumber);
         if (dtInvoiceHistory.Rows.Count==0)
         {
             taInvoiceWork = new InvoiceWorkTableAdapter();
             InvoiceWorkDataTable dtInvoiceWork = new InvoiceWorkDataTable();
             dtInvoiceWork = taInvoiceWork.GetDataByInvoiceNumber(InvoiceNumber);
             
             dtInvoiceHistory.Merge(dtInvoiceWork);
          }
     
          //now you can manipulate the data in dtInvoiceHistory only
          //it will contain data from dtInvoiceWork if necessary
    }

    One of the (many) downsides of using TableAdapters.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Chris Rester Monday, January 04, 2010 6:57 PM
    Thursday, December 31, 2009 4:38 PM

All replies

  • You might want to try the DataSet.Merge() method. Maybe something along these lines:
    public static Invoice GetInvoiceData(string InvoiceNumber)
    {
         taInvoiceHistory = new InvoiceHistoryTableAdapter();
         InvoiceHistoryDataTable dtInvoiceHistory = new InvoiceHistoryDataTable();
         dtInvoiceHistory = taInvoiceHistory.GetDataByInvoiceNumber(InvoiceNumber);
         if (dtInvoiceHistory.Rows.Count==0)
         {
             taInvoiceWork = new InvoiceWorkTableAdapter();
             InvoiceWorkDataTable dtInvoiceWork = new InvoiceWorkDataTable();
             dtInvoiceWork = taInvoiceWork.GetDataByInvoiceNumber(InvoiceNumber);
             
             dtInvoiceHistory.Merge(dtInvoiceWork);
          }
     
          //now you can manipulate the data in dtInvoiceHistory only
          //it will contain data from dtInvoiceWork if necessary
    }

    One of the (many) downsides of using TableAdapters.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Chris Rester Monday, January 04, 2010 6:57 PM
    Thursday, December 31, 2009 4:38 PM
  • I ended up using this approach in another place in my code, then saw your reply. This mostly solves my problems, but your parting comment left me wanting more. Could you point me in the right direction to find a discussion on the downsides of using TableAdapters?

    Thanks,
    Chris
    Monday, January 04, 2010 6:57 PM
  • Hi Chris,

    Try this thread: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/191e8000-3b36-47c0-b906-91080db3b9cb

    In it, I mention a post in my blog where I rant against TableAdapters. Also, in a subsequent post at the end of the above thread, I offer suggestions on creating typed DataSets without adding all the extra TableAdapter stuff.

    And, while you're reading my blog, you might also want to check out my 3-part series on DataAccess classes.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, January 04, 2010 11:51 PM