Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataset sets to nothing 1

Status
Not open for further replies.

asimasm

Programmer
Nov 27, 2000
62
AU
Hi
i have created a ASP page and declared a dataset at class level. I fill this datase at a push button1 code. After this i try to use the same dataset object at another push button2 event but it says that the dataset is empty (Nothing)
Here is my code

Public Class WebForm1
Inherits System.Web.UI.Page
Dim dsClassLevel As DataSet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As DataSet
'With my code I fill the ds with my data here
dsClassLevel = ds 'Assign the class level dsClassLevel
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dim str1 as string
'Here it gives the following exception
'System.NullReferenceException: Object reference not set to an instance of an object.

str1= dsClassLevel.Tables(0).Rows.Count()
End Sub
end class
 
Web pages are stateless. If you want to use the same dataset across postbacks you'll have to store it in somekind of state management. EG Session, Application or cache objects

Something like this would work. I didn't test though. You may find that retrieving the dataset out of session in the page load event is more streamlined, as then it is available for all functions


Public Class WebForm1
Inherits System.Web.UI.Page
Dim dsClassLevel As DataSet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As DataSet
'With my code I fill the ds with my data here
dsClassLevel = ds 'Assign the class level dsClassLevel
'ALSO STORE IN SESSION
Session("ds") = dsClassLevel

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dim str1 as string
'Here it gives the following exception
'System.NullReferenceException: Object reference not set to an instance of an object.

'GET IT OUT OF SESSION
dsClassLevel = Ctype(Session("ds"), dataset)


str1= dsClassLevel.Tables(0).Rows.Count()
End Sub
end class
 
Thanks Stsuing.. i have just migrated to web development so i was thinking as i do it in desktop application..
Thanks for the tip...It solved my problem....
One more point to this...i declared an integer type variable at class level and initialized it in the page load...now when i was testing in it always retains its value....is that because of junk memory value or is this only true for objects?
 
The page load event always fires when a page is served up. It one part of the page life cycle.

Look up the IsPostBack property of the Page class, you may need it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top