Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# question

Posted on 2011-03-21
3
Medium Priority
?
232 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

824 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