dataset

  • Thread starter Thread starter James T.
  • Start date Start date
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!
 
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
 
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
 
Back
Top