Solved

C# question

Posted on 2011-03-21
3
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

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 75

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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