C# – Shared DataSet Over Multiple Forms C#

cdataset

So im working on a database project, in my first form i have sql connection command which accesses my datasource. Ive also created a dataset this is all in my displayform

The displayform is used for displaying the database, i've added a button to add records, so when i click on add records it would go to the addform where i can fill in the details to creating a new contact. Then go back to the first form to display the newly created contact and all others.

However i'm having a bit of a problem as the dataset needs to be the same as the one in the display form.

How can i make the dataset be the same across all my forms?

UPDATE:

So what ive done is in my Program.cs ive created the objects there… and made them public static.

public static DataSet ds = new DataSet();

so then in my addcontact form i can call it like this…

Program.ds.Clear();

Same with my dataadaptor/bindingsource and sql connection. Is this ok to do?

Best Answer

Create a dataset class add pass in through the constructor to every form.. A "Class = Class" makes a reference .. not a copy. ( DataSet is a Class... )

public partial class Form1 : Form
{
DataSet _dataset;

public Form1(DataSet dataSet)
{
    _dataset = dataset;
    InitializeComponent();
}
//..

public partial class Form2 : Form
{
DataSet _dataset;

public Form2(DataSet dataSet)
{
    _dataset = dataset;
    InitializeComponent();
}
//..


static class Program
{
    static void Main()
    {
        DataSet DS = new DataSet();

        Application.Run(new Form1(DS));
    }
}
Related Topic