Resolved Switching datasets at runtime

ecaf

Member
Joined
Dec 1, 2008
Messages
20
Programming Experience
1-3
Hi - I've been trying to search on this question but I can't find anything that matches what I'm trying to do

Basically using Visual web gui controls (for .NET 2.0 build 6.1.4)
I have a number of tabs on a form with a DataGrid which is connected to BindingSource1

BindingSource1 is connected to a dataset which I set up to have a number of tables / table adapters from one database.
What happens on the "TabControl1_SelectedIndexChanged" is that it selects the case of the tab index and based on this repopulates the Datagrid with a different table adapter.
EG:
VB.NET:
Case 1
                BindingSource1.DataMember = "Speciality1"
                Speciality1TableAdapter1.Fill(MaintDataSet1.Speciality1)
Case 2 
                BindingSource1.DataMember = "Users"
                UsersTableAdapter1.Fill(MaintDataSet1.Users)
Case (etc.)

This is working perfectly, but now I want to add a 2nd dataset which points to tables on another database.
I have added radio buttons and when clicking on them I want to say something like:
MaintDataSet1.DataSetName = DataSet2
and then set the
BindingSource1 = OtherTableAdapter.Fill(MaintDataSet1.OtherTable) ....

But it doesn't work, I'm not even sure if this is the correct way to do it?
Can anyone advise? Thanks.
 
Last edited:
if they are the same tables on the other database, just change the connection string and fill them again.

There is nothing stopping you from altering the BindingSource1.DataSource = (another dataset) property
 
Ok I don't know if I'm getting there or going in the wrong direction - this is what I have so far:

VB.NET:
Private Sub optApp0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optApp0.CheckedChanged
        Dim PH_data As DataSet
        PH_data = New MaintDataSet_PH

        TabControl1.TabPages(0).Text = "Source of Injury"
        'MaintDataSet1.DataSetName = "MaintDataSet_PH"
        BindingSource1.DataSource = PH_data

        BindingSource1.DataMember = "SourceofInjury"
        BindingSource1.ResetBindings(True)
        sourceofinjurytableadapter.Fill(PH_data.sourceofinjury)


    End Sub

The last line: sourceofinjurytableadapter.Fill(PH_data.sourceofinjury)
is showing 2 errors
1. Sourceofinjurytableadapter is not declared - I thought it was because it is in my MaintDataSet_PH.xsd ???

2. .Fill(PH_data.sourceofinjury) also has a line under it, the error reads: 'Sourceofinjury is not a member of System.Data.Dataset' - obviously I haven't done something right with the declaration of PH_Data?

Any ideas as to where I've gone wrong? Thanks.
 
Hi - just to let you know I have figured it out.
I needed to do a few declarations in the FormName.Designer.vb file
I had to put in declarations
Friend WithEvents MaintDataSet_PH As MaintDataSet_PH
Friend WithEvents SourceOfInjuryTableAdapter1 As MaintDataSet_PHTableAdapters.SourceOfInjuryTableAdapter

and also had to initialise the components for both.
I have it working now with a little modification to the code above to use MaintDataSet_PH instead of PH_Data (which wasn't required), and SourceOfInjuryTableAdapter1 instead of SourceOfInjuryTableAdapter (which is in the actual .xsd file).

Don't know if this will help anyone else, but at least mine is working, so thanks for the point in the right direction.
 
1. Sourceofinjurytableadapter is not declared - I thought it was because it is in my MaintDataSet_PH.xsd ???
The XSD shows types, not instances. Creating a new dataset does not also create tableadapters; they are in a separate namespace and must be created as separate instances. They are shown alongside the DataTables in the dataset designer for convenience, not as a statement that making a dataset also makes all the necessary tableadapters

2. .Fill(PH_data.sourceofinjury) also has a line under it, the error reads: 'Sourceofinjury is not a member of System.Data.Dataset' - obviously I haven't done something right with the declaration of PH_Data?

Any ideas as to where I've gone wrong? Thanks.
TableAdapters get their own namespace, typically DATASET_NAMETableAdapters, viz:

Dim ds as New AbcDefDataSet
Dim dt as New AbcDefDataSet.AbcDataTable
Dim ta as New AbcDefDataSetTableAdapters.AbcTableAdapter
 
Thanks! The visualwebgui was auto generating the code for me originally (in FORMNAME.designer.vb file), but when I added the new dataset it didn't, and I hadn't fully understood the concept of what was happening.

I do now a little :eek: - I'm sure it will get better as I get used to using it!
 
Back
Top