How to Use a Global Typed Dataset (VB.NET 2005 WinForms)

I am missing something important here.  I need a global typed DataSet that I can fill, and then use it for Grids and Crystal Reports on other forms. I seems to make sense to instantiate the DataSet objects in the Load of a Parent form (when users are in this part of the app).  The psuedo code shows my basic approach, but something is wrong (some spelling might be off).

Any assistance and understanding appeciated.

In Project Dataset:
   DsResults (DataSet)
   dt_Results  (typed DataTable)
   dt_ResultsTableAdapter (TableAdapter)
   dt_ResultsTableAdapter.FillResults(<datatable>, <parmWhere>)
 
Public Class clsLookup  (also tried making these Shared)
    Public dsLookup As DsResults
    Public DT_Results As DsResults.dt_Results
    Public DT_ResultsTableAdapter As DsResultsTableAdapters.dt_ResultsTableAdapter
 
Public Class clsGlobal
    Public Shared gobjgLookup As clsLookup
    Public Sub New()
    End Sub
 
USAGE: (parent form Load)
Imports App2.clsGlobal
Imports App2.clsLookup
clsGlobal.gobjLookup = New clsLookup
 
USAGE: (user initiated lookup)
Imports App2.clsLookup
Imports App2.clsGlobal
clsGlobal.gobjResults.DT_ResultsTableAdapter.FillResults(clsGlobal.gobjResults.DT_Results, strWhere)
(ERROR: Object reference not set to instance of an object)

Open in new window

cgaDesignAsked:
Who is Participating?
 
anyoneisConnect With a Mentor Commented:
cgaDesign;

RE: "clsGlobal.gobjResults.DT_Results"

I think you simplified too much, since clsGlobal does not have a gobjResults property. In any case, where has you created an instance of clsGlobal?

David

0
 
anyoneisCommented:
Why does my keybroad do that!?!?

"has you created an instance" s/b "have you created an instance"
0
 
cgaDesignAuthor Commented:
Re: where is instance of clsGlobal created ... I could not find it so far ... I have been using the "class" for global variable, and since they are declared as Shared, I don't think I needed an instance of clsGlobat.  So maybe this is part of the problem for my new global variable: gobjResults.  In general, do you think I am approaching the global dataset issue in a reasonable manner?
Thank you, Steve
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
anyoneisConnect With a Mentor Commented:
Steve;

I think you are close, but perhaps what you want is a shared default constructor. This will be run the first timje the Class is referenced. In that constructor, you can initialize a shared property with the reference to the dataset.

Then, when you reference ClassName.PropertyName for the first time, the static (shared) class constructor is run, and it initializes the property with an instance of yourtyped fdataset. Then the property accessor is run, delivering it up to you.

David
0
 
anyoneisConnect With a Mentor Commented:
Here is an example VB Console program:
Module Module1
 
    Class gblMyClass
 
        Private Shared propMyProperty As String
 
        Public Shared ReadOnly Property MyProperty()
            Get
                Console.WriteLine("MyProperty accessor being called")
                Return propMyProperty
            End Get
        End Property
        Shared Sub New()
            Console.WriteLine("Shared ctor being called")
            propMyProperty = "Global data"
        End Sub
 
    End Class
    Sub Main()
        Console.WriteLine("Main beginning called")
        Console.WriteLine("Shared global object value is: '{0}'", gblMyClass.MyProperty)
        Console.WriteLine("Main ending")
    End Sub
 
End Module

Open in new window

0
 
anyoneisCommented:
Resulting in:

Main beginning called
Shared ctor being called
MyProperty accessor being called
Shared global object value is: 'Global data'
Main ending


0
 
cgaDesignAuthor Commented:
Your first hint helped ...
1.  I am now only using clsLookup to hold my Public DataSet objects. I instantiate the DataSet, DataTable and TableAdapter objects in a Parent form (but I might as well move these to the Sub New, as you are suggesting).  I actually don't think I need the DataTable object, as it is usually referenced thru the DataSet obj.  I am now filling my DataGridViews with this scenario.

2.  I now need to hook up the Global DataSet to my Crystal Reports ... which I think I can do ... just set the Report DataSource at Runtime.  (But I don't want to loose the DesignTime DataSource).

3.  STILL A PROBLEM:  I set a Tag field in the Global DataTable via a DataGridView (via a BindingSource).  I need the Reports to use this Global DataTable, but not to print rows that are NOT TAGGED.  Maybe this is for a separate post ... but is there a way to Filter a DataTable so the a Report uses the Filter, or so that the Report doesn't see the un-Tagged rows ?

Thank you again ...

0
 
anyoneisConnect With a Mentor Commented:
I'm not knowledgeable about Crystal Reports. But, when I need to filter the results in a table, I usually do something like:

dtMyTable.DefaultView.RowFilter = "Tag = 0"

David
0
 
cgaDesignAuthor Commented:
David, thanks very much for all the useful thoughts, which solved my problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.