Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# question

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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