Help me understand dataset scope

NewToDotNet

New member
Joined
Oct 19, 2006
Messages
2
Programming Experience
1-3
I am new to the dot net world and I am trying to develop an application that uses a MDI interface. I would like to load data from an Access database into a dataset and use it throughout the rest of my aplication. I am able to populate the dataset from the main form but I cannot seem to bind the dataset to any controls in the child forms. I have decalred the dataset as a public in the main form. In designer view on the child form the data set appears in the Datasource property of the combo box I am trying to fill. I can select all the right field but when I go to run the application, there is no data in the combo box. Am I missing a basic concept regarding the scope of datasets?

Thanks to anyone who might mbe able to help!
 
A DataSet that you've created in the parent form cannot possibly appear in the designer for the child form because at design time the child form knows absolutely nothing about the parent form. This is straight OOP. If the parent form creates an object, e.g. a DataSet, and you want the child form to be able to use that object then the parent form must pass the child form a reference to that object. If this child form is ALWAYS going to require this DataSet then the logical place to pass this reference is to the constructor. You write a constructor in the child form that takes a DataSet as an argument. Now you have a reference in code to the DataSet that the parent will pass at run time. You cannot use it in the designer. It must all be done in code, so you need to set the DataSource of your controls, etc. in code. Now when the parent form creates an instance of the child form it passes the DataSet as a parameter. For example, in the child form:
VB.NET:
Public Sub New(ByVal data As DataSet)
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    'Use data here to populate controls, e.g.
    Me.DataGridView1.DataMember = "TableName"
    Me.DataGridView1.DataSource = data
End Sub
and in the parent form:
VB.NET:
Dim child As New Form2(Me.DataSet1)

child.MdiParent = Me
child.Show()
 
Thanks for the advice!

Although new to .Net - I have developed in VB6 and the OOP was what was throwing me. Your simple explanation helped me understand the concepts I needed to start developing apps in .NET. Thank you so much for taking the time to reply, I hope I can return the favor some day.
 
The OO aspect is usually the biggest stumbling block for VB6 developers moving to VB.NET. The most important thing you can remember is that OOP is designed to mimic real life. In real life you cannot use an object until you have created one. You know what a house is but you cannot live in the concept of a house. You must build one. OOP is the same. The class definition tells you what a type is but you cannot do anything until you have created an instance of that type. Also, remember that the forms you create in the designer are NOT objects. They are merely visual representations of the class you have just defined. Again, you must create instances of that type at run time to use them. If you were a designer at a car manufacturer and you designed a new car on paper could you get in and drive it? Of course not. You'd have to build car objects first. The form designer is the paper on which you draw your design. These are the sorts of concepts you need to remember when using an OO language.
 
Back
Top