Link to home
Start Free TrialLog in
Avatar of rctems
rctemsFlag for Australia

asked on

Dataset null or Clear

What is the difference between

thedataset = null;

and

thedataset.Clear();

Ryan Upton
MBA (adv)
BE(hons)
RCT

{Links to personal websites removed -- mbizup, Access ZAPE 06-Feb-08}

Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Hi rctems

- thedataset = null;
This statement sets the variable to not point to any object in the memory. That means the object on the heap in the memory is no more referenced by a variable in your application and you cannot reach it, but it is still in memory.
In this case the Garbage Collector will remove it from memory after a while which cannot be predicted.

- thedataset.Clear();
This statements removes all the objects within the dataset (tables, relations, ...) and resets it.
Avatar of rctems

ASKER

I'm cycling around and displaying data in a table.

I'm using the dataset as a local buffer to store searches from a query. It sounds like your are saying that setting it to null is going to be slower and less reliable that using the clear function.

Ryan
If you set the DataSet to Null, then the next time you need it, you will need to re-create the DataSet object, before you can populate it.  If you clear the Dataset, the object still exists, it is just empty, s you will not need to recreate it, before you again popluate it.

AW
Avatar of rctems

ASKER

I appreciate your help with this

I'm using a heiracherial table which uses relationships and when I clear it I'm finding the relationships interfere with the re-population of the dataset with data

Ryan
Try theDataSet.Relations.Clear();

That's much more efficient than sending another to the garbage collector.

Jim
Avatar of rctems

ASKER

This is the error

Cannot remove table rc_pivot_sales_parent, because it referenced in ForeignKeyConstraint rel1.  
Remove the constraint first.

And this is the code

 if (thedataset == null)
           thedataset = new DataSet();

thedataset.Relations.Clear();
thedataset.Tables.Clear();
thedataset.Clear();

It breaks on thedataset.Tables.Clear();

Ryan
If you do not have a lot of tables, you could delete them in the reverse order in which you added them. Then there can be no FK reference when you remove the referenced table.

Jim
Ryan - I just noticed your timezone. I had the opportunity to visit Darwin and Sydney in 1968. Great country and very friendly people. I thoroughly enjoyed my visit.

Jim
Avatar of rctems

ASKER

Thats good to hear!

Just trying your solution now

Ryan
Avatar of rctems

ASKER

I still get the problem I'm afraid

here is the error

Cannot remove table z_ggchld, because it referenced in ForeignKeyConstraint rel3.  Remove the constraint first.

and here is the code

               if (thedataset == null)
                    thedataset = new DataSet();
                else
                {

                    thedataset.Relations.Clear();
                    if (thedataset.Tables != null)
                    {
                        thedataset.Tables.Remove("z_ggchld");
                        thedataset.Tables.Remove("z_gndchld");
                        thedataset.Tables.Remove("z_child");
                        thedataset.Tables.Remove("rc_pivot_sales_parent");
                    }

                    thedataset.Clear();
                }
Can you show how you are adding the FK constraints? I have added PK constraints, but not FK constraints.
what are the relations that you have created?

What tables are involved in ForeignKeyConstraint rel3?


AW
Avatar of rctems

ASKER

Sure !.....

        private DataSet getDataFromAreas(DataSet thedataset, bool gotdata, UInt16 tops, string ytd, UInt16[] areascode, string mflag, bool selectall)
        {
            if (!gotdata && (areascode != null) )
            {
               
                /*if (thedataset.Relations.Count != 0)
                {
                    thedataset.Tables[0].Constraints.Clear();
                    thedataset.Tables[1].Constraints.Clear();
                    thedataset.Tables[2].Constraints.Clear();
                    thedataset.Tables[3].Constraints.Clear();
                    thedataset.Relations.Remove("rel1");
                    thedataset.Relations.Remove("rel2");
                    thedataset.Relations.Remove("rel3");
                    thedataset.Relations.Clear();
                    thedataset.Tables.Clear();
                }
                thedataset.Clear();*/

                //  do{} while()


                if (thedataset == null)
                    thedataset = new DataSet();
                else
                {

                    thedataset.Relations.Clear();
                    thedataset.Relations.Remove("rel1");
                    thedataset.Relations.Remove("rel2");
                    thedataset.Relations.Remove("rel3");


                    if (thedataset.Tables != null)
                    {
                        thedataset.Tables.Remove("z_ggchld");
                        thedataset.Tables.Remove("z_gndchld");
                        thedataset.Tables.Remove("z_child");
                        thedataset.Tables.Remove("rc_pivot_sales_parent");
                    }

                    thedataset.Clear();
                }
                DataTable objTable1 = new DataTable();
                DataTable objTable2 = new DataTable();
                DataTable objTable3 = new DataTable();
                DataTable objTable4 = new DataTable();

                int numberobjTable1rowscheck = 0;
                int numberobjTable2rowscheck = 0;
                int numberobjTable3rowscheck = 0;
                int numberobjTable4rowscheck = 0;

                Random randnum = new Random();

                UInt32 numberobjTable1rows = 136;
                UInt32 numberobjTable2rows = 753;
                UInt32 numberobjTable3rows = 9581;
                UInt32 numberobjTable4rows = 38236;

                objTable1.Columns.Add("rel1", typeof(UInt32));
                objTable1.Columns.Add("Current_Area", typeof(string));
                objTable1.Columns.Add("Current_MFlag", typeof(string));
                objTable1.Columns.Add("AccNum", typeof(string));
                objTable1.Columns.Add("Customer", typeof(string));
                objTable1.Columns.Add("TotalOfNettTotal", typeof(string));
                objTable1.Columns.Add("YTD 20070630", typeof(string));
                objTable1.Columns.Add("YTD 20060630", typeof(string));
                objTable1.Columns.Add("YTD 20050630", typeof(string));
                objTable1.Columns.Add("ridtmp", typeof(string));

                for (UInt32 i = 0; i <= numberobjTable1rows; i++)
                {
                    DataRow objRow1 = objTable1.NewRow();

                    objRow1[0] = i;
                    objRow1[1] = areascode[0];//randnum.Next(100);
                    switch (randnum.Next(2))
                    {
                        case 0:
                            objRow1[2] = "K";
                            break;
                        case 1:
                            objRow1[2] = "IND";
                            break;
                        case 2:
                            objRow1[2] = " ";
                            break;
                    }

                    objRow1[3] = randnum.Next(6000).ToString();
                    objRow1[4] = "Test Customer " + i.ToString();
                    objRow1[5] = randnum.Next(8000).ToString();
                    objRow1[6] = randnum.Next(8000).ToString();
                    objRow1[7] = randnum.Next(8000).ToString();
                    objRow1[8] = randnum.Next(8000).ToString();

                    objTable1.Rows.Add(objRow1);
                }

                objTable1.AcceptChanges();

                objTable2.Columns.Add("rel1", typeof(UInt32));
                objTable2.Columns.Add("rel2", typeof(UInt32));
                objTable2.Columns.Add("CAT", typeof(string));
                objTable2.Columns.Add("TotalOfNettTotal", typeof(string));
                objTable2.Columns.Add("YTD2007", typeof(string));
                objTable2.Columns.Add("YTD2006", typeof(string));
                objTable2.Columns.Add("YTD2005", typeof(string));
                objTable2.Columns.Add("ridtmp1", typeof(string));
                objTable2.Columns.Add("ridtmp", typeof(string));
                objTable2.Columns.Add("ridparent", typeof(string));
                objTable2.Columns.Add("ridchild", typeof(string));
                objTable2.Columns.Add("AccNum", typeof(string));
                objTable2.Columns.Add("Current_Area", typeof(string));
                objTable2.Columns.Add("Current_MFlag", typeof(string));


                for (UInt32 i = 0; i <= numberobjTable2rows; i++)
                {
                    DataRow objRow2 = objTable2.NewRow();

                    objRow2[0] = decimal.Round(i * numberobjTable1rows / numberobjTable2rows);
                    objRow2[1] = i;
                    objRow2[2] = "Stock Cat " + i.ToString();
                    objRow2[3] = randnum.Next(8000).ToString();
                    objRow2[4] = randnum.Next(8000).ToString();
                    objRow2[5] = randnum.Next(8000).ToString();
                    objRow2[6] = randnum.Next(8000).ToString();

                    objTable2.Rows.Add(objRow2);
                }
                objTable2.AcceptChanges();

                objTable3.Columns.Add("rel2", typeof(UInt32));
                objTable3.Columns.Add("rel3", typeof(UInt32));
                objTable3.Columns.Add("StCode", typeof(string));
                objTable3.Columns.Add("APT", typeof(string));
                objTable3.Columns.Add("Descrip", typeof(string));
                objTable3.Columns.Add("TotalOfNettTotal", typeof(string));
                objTable3.Columns.Add("YTD2007", typeof(string));
                objTable3.Columns.Add("YTD2006", typeof(string));
                objTable3.Columns.Add("YTD2005", typeof(string));
                objTable3.Columns.Add("ridchild1", typeof(string));
                objTable3.Columns.Add("ridtmp", typeof(string));
                objTable3.Columns.Add("ridgndchld", typeof(string));
                objTable3.Columns.Add("ridchild", typeof(string));
                objTable3.Columns.Add("AccNum", typeof(string));
                objTable3.Columns.Add("Current_Area", typeof(string));
                objTable3.Columns.Add("Current_MFlag", typeof(string));
                objTable3.Columns.Add("CAT", typeof(string));
                objTable3.Columns.Add("rel1", typeof(string));

                for (UInt32 i = 0; i <= numberobjTable3rows; i++)
                {
                    DataRow objRow3 = objTable3.NewRow();

                    objRow3[0] = decimal.Round(i * numberobjTable2rows / numberobjTable3rows);
                    objRow3[1] = i;
                    objRow3[2] = "Stock Code " + i.ToString();
                    objRow3[3] = "APN " + i.ToString();
                    objRow3[4] = "Stock Description " + i.ToString();
                    objRow3[5] = randnum.Next(8000).ToString();
                    objRow3[6] = randnum.Next(8000).ToString();
                    objRow3[7] = randnum.Next(8000).ToString();
                    objRow3[8] = randnum.Next(8000).ToString();

                    objTable3.Rows.Add(objRow3);
                }
                objTable3.AcceptChanges();


                objTable4.Columns.Add("rel3", typeof(UInt32));
                objTable4.Columns.Add("rel4", typeof(UInt32));
                objTable4.Columns.Add("YTD", typeof(string));
                objTable4.Columns.Add("Ac No", typeof(string));
                objTable4.Columns.Add("Area", typeof(string));
                objTable4.Columns.Add("Mflag", typeof(string));
                objTable4.Columns.Add("Stock Cat", typeof(string));
                objTable4.Columns.Add("Stock Code", typeof(string));
                objTable4.Columns.Add("APN", typeof(string));
                objTable4.Columns.Add("Stock Description", typeof(string));
                objTable4.Columns.Add("Qty", typeof(string));
                objTable4.Columns.Add("Sales Total", typeof(string));
                objTable4.Columns.Add("ridgndchld1", typeof(string));

                for (UInt32 i = 0; i <= numberobjTable4rows; i++)
                {
                    DataRow objRow4 = objTable4.NewRow();

                    objRow4[0] = decimal.Round(i * numberobjTable3rows / numberobjTable4rows);
                    objRow4[1] = i;
                    objRow4[2] = "30/6/200" + (5 + randnum.Next(2)).ToString(); ;
                    objRow4[3] = randnum.Next(6000).ToString();
                    objRow4[4] = randnum.Next(100);
                    switch (randnum.Next(2))
                    {
                        case 0:
                            objRow4[5] = "K";
                            break;
                        case 1:
                            objRow4[5] = "IND";
                            break;
                        case 2:
                            objRow4[5] = " ";
                            break;
                    }
                    objRow4[6] = "Stock Cat " + decimal.Round(i * numberobjTable2rows / numberobjTable4rows).ToString();
                    objRow4[7] = "Stock Code " + (decimal.Round(i * numberobjTable2rows / numberobjTable4rows)).ToString();
                    objRow4[8] = "APN " + (decimal.Round(i * numberobjTable2rows / numberobjTable4rows)).ToString();
                    objRow4[9] = "Stock Description " + (decimal.Round(i * numberobjTable2rows / numberobjTable4rows)).ToString();
                    objRow4[10] = randnum.Next(100);
                    objRow4[11] = randnum.Next(8000).ToString();


                    objTable4.Rows.Add(objRow4);
                }

                objTable4.AcceptChanges();

                objTable1.TableName = "rc_pivot_sales_parent";
                objTable2.TableName = "z_child";
                objTable3.TableName = "z_gndchld";
                objTable4.TableName = "z_ggchld";

                numberobjTable1rowscheck = objTable1.Rows.Count;
                numberobjTable2rowscheck = objTable2.Rows.Count;
                numberobjTable3rowscheck = objTable3.Rows.Count;
                numberobjTable4rowscheck = objTable4.Rows.Count;



                thedataset.Tables.Add(objTable1);
                thedataset.Tables.Add(objTable2);
                thedataset.Tables.Add(objTable3);
                thedataset.Tables.Add(objTable4);

                thedataset.Relations.Add("rel1",
                    thedataset.Tables["rc_pivot_sales_parent"].Columns["rel1"],
                    thedataset.Tables["z_child"].Columns["rel1"]);
                thedataset.Relations.Add("rel2",
                    thedataset.Tables["z_child"].Columns["rel2"],
                    thedataset.Tables["z_gndchld"].Columns["rel2"]);
                thedataset.Relations.Add("rel3",
                    thedataset.Tables["z_gndchld"].Columns["rel3"],
                    thedataset.Tables["z_ggchld"].Columns["rel3"]);
                thedataset.Relations[0].Nested = true;
                thedataset.Relations[1].Nested = true;
                thedataset.Relations[2].Nested = true;

                thedataset.AcceptChanges();
                return thedataset;
            }
            else
                return null;
        }
ASKER CERTIFIED SOLUTION
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try the following:

thedataset.Reset()
thedataset.Relations.Clear()
Avatar of rctems

ASKER

Awsome you are the smartest guy I know!

Ryan
Thanks
Avatar of rctems

ASKER



thedataset.Reset()
thedataset.Relations.Clear()

But doesn't that clear relations twice?
This is the recommended way from some Microsoft MVP in this topic.