How to pass a Dataset to a custom UserControl and back again to the parent, persisting changes.

Posted on 2011-04-28
Last Modified: 2012-05-11
Hello Experts:

I am using C#, VS 2010 and ADO.Net developing a data base application.

I am struggling with making changes to my dataset.

I populate the dataset from my SQL server db with a table adapter, to get my strongly type dataset we'll call myDataSet

I have a TabControl which acts as a container for a series of custom Usercontrols.

In the constructor of the myTabControl, I populate myDataset using a data access layer.  myDataSet is now populated and ready for consumption by the custom user controls.

When the user selects a tab, I Add() myCustomUserControl to the tabPage.

What I want to do is to pass myDataset to myCustomUserControl in some way so that the user can perform CRUD operations on the dataset in myUsercontrol and keep myDataSet in sync as changes are made.  Right now, I am using a new Table Adapter to update the BD from myUserControl but then I get unexpected behaviour on myDataSet when switching to another tab etc. - espicially when I perform Delete operations on the datarows in myUsercontrol dataset.   (DataRowNotInTable exceptions and myDataset not in sync.)

Is there a slick way of doing this??

Question by:cmh_rwhelan
    LVL 29

    Accepted Solution

    Not 100% sure I understand the problem properly but it seems that your current problem is that you have multiple datasets and they are not in sync.

    OK, I believe you need just one DataSet and probably one DataAdapter that is shared by your Tab control and other User controls. In this case you do all operation on the same object using same dataadapter.

    You may pass instances from your Tab control to user controls using constructors or, e.g. special Set methods like:


    Author Comment

    I am using the model you suggest - I pass the dataset to the constructor of myUsercontrol, but I get a RowNotInTable exception after i have deleted a row in myUserControl dataset  and have returned to another tab and try to access myDataset.
    LVL 1

    Expert Comment

    i think you can close your dataset from tab1 because your user control on tab2 is using the same dataset as tab1 using.

    Author Closing Comment

    Passing the dataset to the constructor of the child usercontrol works.  Keeping track of the state of the dataset is a challenge as I navigate the tabs of the tabcontrol and make changes to the dataset.

    I suspect my model for handling the UI is not very sound.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now