dataset

J

James T.

Hello!

Is it possible to fill a DataSet only once without need to fill it again
every time in custom methods?

Dim myDS As DataSet

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
Dim Products As ProductsData
myDS = Products.GetAll()
DataGrid1.DataSource = myDS
DataGrid1.DataBind()
End If

End Sub

Following code generates an error: "Object reference not set to an instance
of an object."

Private Sub ShowDataGrid2()

DataGrid2.DataSource = myDS
DataGrid2.DataBind()

End Sub


Thanks!
 
J

Joseph Byrns

You could put the dataset in a session variable so:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
Dim Products As ProductsData
myDS = Products.GetAll()
DataGrid1.DataSource = myDS
DataGrid1.DataBind()

Session("TheDataset")=myDS
Else

DataGrid1.DataSource = ctype(Session("TheDataset"),dataset)
DataGrid1.DataBind()

End If

End Sub
 
P

pj

The problem is myDS. You're creating that object only on the first
request. There's nothing in it on subsequent requests. You'll either
need to store myDS somewhere where it persists across requests
(Session, Cache or somewhere like that), or recreate it each time
(i.e. move the line "myDS = Products.GetAll" out of the "If Not
IsPostBack" block.

pj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top