Solved

C# question

Posted on 2011-03-21
3
217 Views
Last Modified: 2012-05-11
Does anyone have any code examples of how to do something like the attached code?

the part I am having issues with is that PublicDataSet.   I want to add 2 tables to this data set at run time.  and make that instance usuable by all the other classes in the application, kind of like a global dataset for the application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;

namespace Abra_DSM
{
    static class AppSettingInfo
    {
        private static string myDataFile = "";
        private static string myStartDate = "";
        private static string myEndDate = "";
        private static bool DataTablePopulated = false;
        private static DataSet MyData;
        public static DataSet PublicDataSet
        {
            get { return MyData; }
            set{ MyData = value; }
        }
        public static string DataFile
        { 
            get { return myDataFile; } 
            set { myDataFile = value; } 
        }
        public static string StartDate
        {
            get { return myStartDate; } 
            set { myStartDate = value; } 
        }
        public static string EndDate
        {
            get { return myEndDate; } 
            set { myEndDate = value; } 
        }
        public static bool PopulatedDataTable
        {
            get { return DataTablePopulated; }
            set { DataTablePopulated = value; } 
        }
    }
}

Open in new window

0
Comment
Question by:Jam9974
  • 2
3 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35181913
You use of static should handle the case "make that instance usuable by all the other classes in the application." As far as adding tables, it should be a simple as:
SomeClass
{
    public void SomeMethod()
    {
            System.Data.DataTable table1 = new System.Data.DataTable("table1");
            System.Data.DataTable table2 = new System.Data.DataTable("table2");

            // Add columns to each table

            AppSettingInfo.PublicDataSet.Tables.Add(table1);
            AppSettingInfo.PublicDataSet.Tables.Add(table2);
    }
}

Open in new window

0
 

Author Comment

by:Jam9974
ID: 35182570
Tried that.
 I get the following error  NullReferanceExpection was unhandled
Object referance not set to an instance of an object.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace Abra_DSM
{
    public partial class Start_Form : Form
    {
       DataTable DataTable11 = new DataTable();
       DataTable Ded_By_Date_Range1 = new DataTable();
       public void CreateTempTabels()
        { 
              //created all the 
             DataTable11.Columns.Add(new DataColumn("Name", typeof(string)));
              AppSettingInfo.PublicDataSet.Tables.Add(DataTable11);
            AppSettingInfo.PublicDataSet.Tables.Add(Ded_By_Date_Range1);
          }
          public Start_Form()
        {
            InitializeComponent();
        }
        private void Start_Form_Load(object sender, EventArgs e)
        {
            CreateTempTabels();
        }
}

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35182598
>>   I get the following error  NullReferanceExpection was unhandled

Ahh yes. I should have clarified. You will need to instantiate the object prior to adding to it:
public void CreateTempTabels()
{ 
    //created all the 
    DataTable11.Columns.Add(new DataColumn("Name", typeof(string)));
    
    AppSettingInfo.PublicDataSet = new DataSet();   <<---  HERE
    
    AppSettingInfo.PublicDataSet.Tables.Add(DataTable11);
    AppSettingInfo.PublicDataSet.Tables.Add(Ded_By_Date_Range1);
}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now