Why is Data loaded into a dataset not available when called from adiffrent method

Olukayode Oluwole
Olukayode Oluwole used Ask the Experts™
on
I am trying to create a dataset which I will  then will feed into crystal report
with the script below

[private void button2_Click(object sender, EventArgs e)
        {
       
            CreateDataSet2();
            // MessageBox.Show("The dataset is created and filled");

            if (_dsData == null)
            {
                MessageBox.Show("You first need to create the data source");
                return;
            }

            string strFullPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "StateCodes.xsd");
            _dsData.WriteXmlSchema(strFullPath);
            //MessageBox.Show($"The schema has been created as >>{strFullPath}<<");

            //Load the standalone report
            // c://TournamentTracker//ReportLibrary//StateCodes.rpt
            //_rptDoc.Load(System.IO.Path.Combine("c://DotNetReports//StateCodes.rpt"));
            _rptDoc.Load(System.IO.Path.Combine("c://TournamentTracker//ReportLibrary//StateCodes.rpt"));
            //Pass the dataset to the report
            _rptDoc.SetDataSource(_dsData);
            //Set values of some objects dynamically
           // ApplyTextObject("txtTitle", "Title of the report provided dynamically by the application");
            //Set the values of the parameters
            //_rptDoc.SetParameterValue("PopulationThreshold", 5000000);

            //Pass the Report Document object to the viewer
            crystalReportViewer1.ReportSource = _rptDoc;

        }[/code]

The First step    CreateDataSet2();

actually fills the dataset _dsData  ( see dataset  set with data below  )

Dataset   _dsData Loaded with Data
_dsData  is defined as a static class   as shown  below:

public static DataTable _dsData;

When i tried to to read _dsData from a different method

the debbugger show that _dsData is empty  (see  screen below)

Dataset Empty  Why ??
Why is there no Data  in _dsData because I am trying to read the

data from a method different method from where i created the dataset

Olukay
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Olukayode OluwoleSystems Analyst

Author

Commented:
Please add C#  as well it was a big omission from me

Olukay
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
you surely have _dsData define at more then one place (which is not really good when you can avoid id).

Your line 277 should read:

_dsData = CreateDataSet2();

Open in new window

Olukayode OluwoleSystems Analyst

Author

Commented:
Thanks for your advise. I have tried the 2 suggestions above but still have some problems  (see  below)

1. Only single definition of _dsData   now in script

Commented  out the local definition  as below
Definition of _dsData  under my click event commented outand ensured _dsData now defined as static class  as below
_dsData  defined as static class2.  Implemented  _dsData = CreateDataSet2();   as  in code below
 [] {
            _dsData = CreateDataSet2();
            // MessageBox.Show("The dataset is created and filled");

            if (_dsData == null)
            {
                MessageBox.Show("You first need to create the data source");
                return;
            }

            string strFullPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "StateCodes.xsd");
            _dsData.WriteXmlSchema(strFullPath);
            //MessageBox.Show($"The schema has been created as >>{strFullPath}<<");

            //Load the standalone report
            // c://TournamentTracker//ReportLibrary//StateCodes.rpt
            //_rptDoc.Load(System.IO.Path.Combine("c://DotNetReports//StateCodes.rpt"));
            _rptDoc.Load(System.IO.Path.Combine("c://TournamentTracker//ReportLibrary//StateCodes.rpt"));
            //Pass the dataset to the report
            _rptDoc.SetDataSource(_dsData);
            //Set values of some objects dynamically
           // ApplyTextObject("txtTitle", "Title of the report provided dynamically by the application");
            //Set the values of the parameters
            //_rptDoc.SetParameterValue("PopulationThreshold", 5000000);

            //Pass the Report Document object to the viewer
            crystalReportViewer1.ReportSource = _rptDoc;

        }[/code]
But then had  _dsData  flagged in my code  as below
_dsData Flagged in the code under my Click eventIs it that my _dsData definition as static class  is wrong or
what am i doing wrong


Olukay
Senior .Net Consultant
Top Expert 2016
Commented:
It is really hard for us just with snippets and screenshots to see the full scopes of your variables.

By default, your variables should always have the smallest scope possible. For example, if your _dsData is not used anywhere else then in the button2_Click event handler, it should be declared only in that scope. You will avoid a lot of issues.
Olukayode OluwoleSystems Analyst

Author

Commented:
I have not really fixed it but will keep trying.

Thanks for letting me know i am on the right path

Olukay

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial