vb.net dataset

  • Thread starter Thread starter douglas
  • Start date Start date
D

douglas

I have a Visual Basic.net 2005 dataset in a desktop application does not
clear out even after I do a dispose on the object. What could cause this
problem? Ho can I empty the dataset?

I am writing a desktop windows Visual Basic.net 2005 desktop application
that generates one excel spreadsheet per unique customer. I run the dispose
method on the dataset object so that I can obtain the data for the next
customer I will populate data on the next spreadsheet. Howver when I update
the data on a new excel spreadsheet for the next customer, the data from the
previous customer appears on the spreadsheet also.
Thus can you tell me what I can do and/or what I need to check for to make
certain the data from the previous customer in the dataset is cleared out?
 
douglas said:
I have a Visual Basic.net 2005 dataset in a desktop application does not
clear out even after I do a dispose on the object. What could cause this
problem? Ho can I empty the dataset?

I am writing a desktop windows Visual Basic.net 2005 desktop application
that generates one excel spreadsheet per unique customer. I run the dispose
method on the dataset object so that I can obtain the data for the next
customer I will populate data on the next spreadsheet. Howver when I update
the data on a new excel spreadsheet for the next customer, the data from the
previous customer appears on the spreadsheet also.
Thus can you tell me what I can do and/or what I need to check for to make
certain the data from the previous customer in the dataset is cleared out?

If you instantiate the dataset new in code each time you use and
populate it, it will be empty, no doubt about it.
 
You are basically saying that everyone time I run the following code a new
dataset will be created? The problem is all the code is in one class. Thus
when I run the dispose code on the dataset the dataset still contains data.
What would you suggest?

Imports System.Xml
Imports System.Data

Public Class Form1
Dim dt As DataTable
Private Sub sub1 (ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim ds As New DataSet
dt = New DataTable()
dt.Columns.Add(New DataColumn("Product_ID",
Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Product_Name",
Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("product_Price",
Type.GetType("System.Int32")))
fillRows(1, "product1", 1111)
fillRows(2, "product2", 2222)
fillRows(3, "product3", 3333)
fillRows(4, "product4", 4444)
ds.Tables.Add(dt)
ds.Tables(0).TableName = "product"
ds.WriteXml("Product.xml")
MsgBox("Done")
End Sub
 
douglas said:
You are basically saying that everyone time I run the following code a new
dataset will be created? The problem is all the code is in one class. Thus
when I run the dispose code on the dataset the dataset still contains data.
What would you suggest?


I would create a function that creates a new dataset and returns an
empty table in the dataset to be used by the calling method.



calling code or method

dim ds as Dataset

ds = CreateProductDS("Product")

populate table with data

write xml from Dataset


--------------------------------------------------

private function CreateProductDS(tblname as string) as Dataset

code to make new Dataset and table in dataset

return DS

end Sub

-----------------------------
 
You misunderstand what Dispose is. It does not clear any data out of the
DataSet.

When a type has a Dispose method, it's there so that the object in question
can perform any necessary clean up tasks so that it is safe for the object
to sit in memory and wait for the Garbage Collector to potentially remove
the object from memory.

In the case of a DataSet, Microsoft gives it a Dispose method in case you
have populated it by reading from an unmanaged resource, like an XML file.
Calling Dispose is not meant to wipe out the data in the DataSet, it's meant
to release any references it has to unmanaged resources, such as that file.

As was suggested, you could just make a new DataSet instance each time you
need new data. In your code below, you are doing that each time Sub1 is
called. But, I suspect that you are calling Sub1, which makes the new
DataSet and then using the "dt" variable outside of Sub1 after that, which
you shouldn't do, because dt will not always point to a new DataTable.

I would declare dt inside Sub1 and at the end of Sub1, have it call another
method that works with the data and have it pass your DataSet reference to
that other method. Each time you have new data, call Sub1 again.

-Scott
 
What kind of dispose method you use.

To dispose data from a dataset you have to use the Clear method.
As you are using the dispose (unmanged releases) method, then look what
Scott is writting about that.

Cor
 
Cor Ligthert[MVP]":

What is the diference between setting a dataset to nothing versus using
the dispose method?

Toi answer your question, I am using the dispose method for an object
that I am currently using.

"wrote:
 
Setting a dataset to nothing means that its reference is set to nothing

To give you a sample.
dim ds as new dataset
...means that an object is created from the class(template) dataset
ds = new dataset
means that an object is created from the class(template) dataset an places
over the reference from ds
the old opbject will be garbaged as soon as nothing references anymore to it
(which is not the case in the sitiation by instance where the dataset is
used in a bindingsource), in those cases you keep mostly two datasets.

The problem you see is mostly because of the fact that a datasource is not
cleared, seems to be buggy in verion 1.1, however set to new, while the
datasource was not set to the new one.

Setting a dataset to nothing has rarely any purpose

Using the dispose (stands for "dispose unmaneged resources") means that you
invoke the method which is inherited from components to remove the unmaneged
resources. However a dataset has (as those are not added) standard no
unmanaged resources.

Cor
 
douglas said:
I have a Visual Basic.net 2005 dataset in a desktop application does not
clear out even after I do a dispose on the object. What could cause this
problem? Ho can I empty the dataset?

I am writing a desktop windows Visual Basic.net 2005 desktop application
that generates one excel spreadsheet per unique customer. I run the
dispose
method on the dataset object so that I can obtain the data for the next
customer I will populate data on the next spreadsheet. Howver when I
update
the data on a new excel spreadsheet for the next customer, the data from
the
previous customer appears on the spreadsheet also.
Thus can you tell me what I can do and/or what I need to check for to
make
certain the data from the previous customer in the dataset is cleared out?

In addition to all the other good advice you've received (not to say this is
particularly good), have you considered the clear() method?

http://msdn.microsoft.com/en-us/library/system.data.dataset.clear.aspx
 
Back
Top