Loading a DataSet in Form_Load

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
I will get shot for this no doubt.:D

I use a "lookup" dataSet, where I store all the tables that only use Select from my SQL database.

Instead of connecting to the DB, and using dataAdapters to fill this dataSet on every form that opens in my app, is there a way to fill all of this lookup data on my application load (I assume I do it when on my 1st form load) ?

I did try this, and then on another form add a Typed dataSet and set a grid to the table, but it came back empty. Surely I don't need to use DA's on every form to load the same information time and time again into the dataSet?

Again, apologises for the stupidity, but something I've never really thought about before.....

Luke
 
Not saying this is right, but you could do something like this:

Load the dataset on main form load event.
Create a public DataTable. In a PublicVariables Module.
VB.NET:
YourDataTable = YourDataSet.Tables("TableName")

Bind your grid or other controls to the Datatable.
 
Last edited by a moderator:
cool, so that DataTable is available to my whole application at any time?

I'm just thinking of cutting the code down thats all. I will have 20+ users of the system, and nearly every form has to load the same Select SQL dataAdapters, which totals a lot of concurrent connections for the same data...I thought this way might be more efficient and easier - could someone confirm please :D

thanks,
Luke
 
Yes, but I have seen some good posts lately discussing 'Global" variables and the pros and cons of making something 'Public'

To late for me, I already have used a PublicVariables Module:

VB.NET:
Module PublicVariables
    Public test As String
    Public YourDatatable As DataTable
End Module

Available to all forms in a project...
 
Thanks. Yeah you've helped me in the past and I use the Module PublicVariables as well, have quite a number that I need to store.

I'll search for some of them posts, but at least my original question has a relatively simple answer!!!

Thanks again,
Luke
 
Instead of declaring YourDataTable = ...... as a Global Variable, I am declaring;
VB.NET:
Friend YourDataSet As DataSet

then on my main form (1st loaded) I am trying to fill this dataset.
Using
VB.NET:
me.daEmployee.Fill(YourDataSet)
OR
VB.NET:
me.daEmployee.Fill(YourDataSet.Tables("Employee")

neither work, and throw error "Value cannot be null. Parameter Name: dataSet" or "Object reference not set to an instance...." respectively.

However, if I generate a typed DataSet, and fill this
VB.NET:
me.conemployee.open
me.daemployee.fill(dataset1.employee)
me.conemployee.close

and then add
VB.NET:
dataSetEmployees = me.dataSet1

It works perfectly!!!

Is there a way of not having to fill the typed dataSet, instead create and fill the Global dataSetEmployees?

Thanks
Luke

UPDATE: Have found a solution in one of my books. Seems that I have to fill the Typed dataset first anyway, then set this dataset as a global variable.
However, I don't really mind as it works flawlessly, and has reduced the dataAdapters on each of my forms, and cleared out at least 20 lines of code on each form...so can't complain!
 
Last edited:
VB.NET:
Friend YourDataSet As [SIZE=7]New[/SIZE] DataSet


if you dont tell vb to make a new one, then its default is null.. hence:​

VB.NET:
Dim myNullReference As DataSet
TableAdapter.Fill(myNullReference)
"Value cannot be null. Parameter Name: dataSet"​

== you supplied a variable to the fill method, and it was null/nothing​

VB.NET:
Dim myNullReference As DataSet
TableAdapter.Fill(myNullReference.Tables("table")
)
"Object reference not set to an instance...."​

==you attempted to access the .Tables() proeprty of a null/nothing object​
 
Last edited by a moderator:
if you had the designer make you a GetData method too then you can use that:

VB.NET:
Dim myDataTable as DataTable = myTableAdapter.GetData()

but if youre using the FIll method, then the object you pass in needs to exist :)


ps, before when i said Dim myNull as DataSet, i meant DataTable..
 
Last edited by a moderator:
I created a class that has the data adapter, datatable, and dataview in them for each of my tables, then I globally declare an instace of the class in a module.
VB.NET:
Public Class AgentBanks
    Inherits Utilities

    Private cAgentBankID As Integer
    Private cAgentBankShortName As String
    Private cAgentBankLongName As String
    Private cAgentBankIsActive As Boolean
    Private cAgentBankDataAdapter As New DSetTableAdapters.tblAgentBanksTableAdapter
    Private cAgentBankDataTable As New DSet.tblAgentBanksDataTable
    Private cAgentBankDataView As New DataView(cAgentBankDataTable)
.
.
.
public properties
.
.
.

VB.NET:
Public Global_AgentBank As New CNCIData.AgentBanks

Now anywhere at anytime I can do Global_AgentBank.DataAdapter.Fill(Global_AgentBank.DataTable) (for example "on load" of my MDI form perhaps) and all forms can bind to that datatable in that class instance and never have to refill.
 
does this cause you any concurrency issues? (do you use it to share data between forms?) or do you use it only for lookup tables?
 
just to resurrect this and ask you some more questions about it..

i'm trying very much to get a similar class arrangement out of my existing dataset. the dataset has a load of lookup tables present on the surface, so it is naturally instantiating them every time it is created. every form creates and instance of the dataset, so i really want to centralise the lookup tables..

i can declare a module that has a globally available instance of my dataset and only load the lookup tables with data, but im then caught as to how to make it useful, as i cannot change the dataset initialisation in any way to make these tables used

with the way you describe here, can you actually use your modular class and the datatables within it, to drive things like combo boxes? at the moment I have comboboxes that have a display member and valuemember from the lookup table, and a SelectedValue property of the main table that needs to hold the data. If i put my lookup tables elsewhere could i achieve my goal of still using them to drive a databound combo in this fashion?
 
I didn't get an email on your reply, sorry for the delay on my response :)

The answer to your question is, yes you can use that instance of the global class/table to drive all combo boxes. Or you could go class-less like this:

Do something like this as a global variable in a module:
VB.NET:
    'Contacts
    Public Global_ContactDataTable As New CNCIData.DSet.tblContactsDataTable
    Public Global_ContactDataAdapter As New CNCIData.DSetTableAdapters.tblContactsTableAdapter
    Public Global_ContactDataView As New DataView(Global_ContactDataTable)

On the first form of your application do this:
VB.NET:
Global_ContactDataAdapter.fill(Global_ContactDataTable)

Now you have a global datatable full of lookup data that can be used on any form, anytime without reloading. If you do add to the datatable, just refill the global and all forms will be updated since they point to one global datatable.

On each form that you have the dropdownlists, just do this:
VB.NET:
ddlList.datasource = Global_ContactDataTable
 
cool..

i'm actually trying this, by having a module called Universe, that has a publicly accessible instance of a dataset called LookupDS

LookupDS is a cut down version of the main dataset in that it only contains lookup tables. Right now, in the LookupDS Initilized event, i'm trying to populate it and it;s crashing with a null ref. Weird thing is i'm doing it just like you have here:
VB.NET:
Dim x as New LookupDSTableAdapters.SalutationLookupTableAdapter
x.Fill(LookupDS.SalutationLookupTable) 'null ref crash!
x is a valid object, so is LookupDS.SalutationLookupTable - neither are null.. so im wondering what else i havent set.. but i started a new post on it..
 
Last edited by a moderator:
Back
Top