Solved

C# question

Posted on 2011-03-21
3
219 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 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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