CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2010
    Posts
    5

    Beginner DataSet/DataTable synchronization question

    Sorry for asking such a basic question but I'm relatively new to database programming and none of the sample code I've looked at seems to handle this one. I was wondering how people generally keep the data across multiple DataSet/DataTable objects synchronized? (For the record I'm using C# and SQL Server Express).

    For example, let's say I'm developing a C# application where the user can have multiple pages open at a time. One page (say) is a list of products and another is a list of orders, and on the orders page the user can select the product used for that order from a drop-down list. I would assume that each of these pages would have their own DataSet object that would get filled by calling the appropriate TableAdapter member function, but if the user adds a new product to the product page and saves it then it won't immediately appear as an option in the orders page until the order has somehow been updated, refreshed or merged.

    So my question is, in .NET how is this usually achieved? Is it simply a case of manually updating/merging all currently active DataSets in your application? If so then would this simply (or not-so-simply) involve setting up a centralized object somewhere that all active DataSets (or rather their wrapper classes) communicate through so they know when to update themselves? And what about the case where I have multiple users all logging into the same database from different machines, is that what SQL NS is supposed to help manage or is there some better mechanism?

  2. #2

    Re: Beginner DataSet/DataTable synchronization question

    Usually this is done by creating a business layer that manages a single DataSet/DataTable etc. This business layer then fires events when a new row, etc is added and your UI listens to these events and responds accordingly.

    No application really needs to be going out and hitting the database directly from the UI code.

  3. #3
    Join Date
    Jun 2010
    Posts
    5

    Re: Beginner DataSet/DataTable synchronization question

    Thanks for the response Matt.

    Ok, so just to be 100% clear about this, lets say my user opens up two different pages that query an order but request different information about it e.g. {OrderSize, OrderCost} and {OrderSize, OrderSupplier}. The data return type is different (and in other cases won't even be about the same order #) so the results have to be returned in different DataTables, let's call them DataTableA and DataTableB. Now on one of those pages the user changes the order size. The UI propagates that through to DataTableA and it's the business layer that needs to detect this and propagate it through to DataTableB, which in turns causes any UI controls that are databound to DataTableB to also update themselves, correct? In other words, the business layer detects changes to the DataTables and updates other DataTables that are working with that same data?

    If that's correct then is there some magical way that this happens? Or is it just a matter of adding a RowChanged event handler in the business layer and responding to it by calling Merge() on all the other DataTables that might be working with the same data?

  4. #4

    Re: Beginner DataSet/DataTable synchronization question

    the idea is that you do not give the UI a DataTable you give it a list of objects. If you have two "Tables" that you are looking at but one or more of the rows in those tables are the same, they should both be refrencing the same object. Then when the value of one of the objects changes the object should fire an event, and the UI should take appropriate action.

    Here is a good example of how to bind objects rather that tables to a datagridview

    http://msdn.microsoft.com/en-us/library/y0wfd4yz.aspx

  5. #5
    Join Date
    Jun 2010
    Posts
    5

    Re: Beginner DataSet/DataTable synchronization question

    Quote Originally Posted by matthewsanford@gmail.com View Post
    the idea is that you do not give the UI a DataTable you give it a list of objects.
    Ah! Ok, so please correct me if I'm wrong but that would in turn imply the following:

    1) The business layer needs to perform an intermediate translation step to convert records from the DataTable(s) to these data objects that the UI works with.

    2) The business layer needs to cache records loaded from the database so that multiple versions of the same object aren't created.

    3) My UI needs to inform the business layer when it's finished working with a particular record so that the business layer cache doesn't continue to fill up.

    4) All that nice fluffy interface stuff in Developer Studio that allows you to drag tables from the DataSources panel and have it automatically create UI objects and hook them directly up to DataSet/DataTable objects is basically useless for a project of any complexity higher than a college students final year project.

    Yes?

  6. #6

    Re: Beginner DataSet/DataTable synchronization question

    You got it!

    Think about it when you have a large scale application how difficult it is to maintain your code when your database code is spread all over the UI. Think about how difficult it would be to change out your UI or make changes to your database.

    The secret of good programing is always isolation and encapsulation. The problem with most schools is that the students do not have to "live" with the code that they write. They just write whatever gets the job done, which is ok I guess but in the real world you have to comeback and be able to make and isolate changes, which makes them very poor at using encaspulation.

    Most large scale applications consist of at least three very seperate and independant layers. The "Presentation" layer, The "Business" Layer and The "Data" Layer.

    Think about if I had written an application using this model. Then a year or so later, I needed to make a Web Application, the only thing that would need to be written is the "Presentation Layer" using the established foundation. If I had tied all my database stuff in the UI, then it would be a complete re-write... Get it?

    PS
    These are great questions, kudos to you for using your head.
    Last edited by matthewsanford@gmail.com; June 18th, 2010 at 08:51 AM.

  7. #7
    Join Date
    Jun 2010
    Posts
    5

    Re: Beginner DataSet/DataTable synchronization question

    Awesome. Thanks Matt, the guidance has been much appreciated.

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