dataset persistence within a web form

  • Thread starter Thread starter MattB
  • Start date Start date
M

MattB

I have a web form with quite a bit of code in the codebehind. I'm declaring
a dataset as Protected so I can access it from different events within the
form without having to regenerate it. I'm having a problem and realized that
this protected dataset seems to loose it's tables when I go from the
page_load event to a button click event.

I don't think I'm actually doing anything to the dataset that I can see, I
just stepped through it and if I watch the table count, it goes from 2 to 0
when it hits the click event. Am I wrong to think a dataset should persist
from event to event if declared as protected? Anyone got an explanation or
better yet a good solution?

Should I serialize/deserialize it to a protected string variable instead?

Thanks!
 
MattB said:
I have a web form with quite a bit of code in the codebehind. I'm declaring
a dataset as Protected so I can access it from different events within the
form without having to regenerate it. I'm having a problem and realized that
this protected dataset seems to loose it's tables when I go from the
page_load event to a button click event.

I don't think I'm actually doing anything to the dataset that I can see, I
just stepped through it and if I watch the table count, it goes from 2 to 0
when it hits the click event. Am I wrong to think a dataset should persist
from event to event if declared as protected? Anyone got an explanation or
better yet a good solution?

Should I serialize/deserialize it to a protected string variable instead?

Within the same request, the DataSet is persisting. In fact, you could
declare it as Private, and it wouldn't make a difference.

Something else must be trashing the DataSet.
 
How strange. I wonder if my declaration is doing it somehow. Can that be
executed again when button_click happens?

I have this right below the "Web Form Designer Generated Code":
Protected dsItem As New DataSet, dsModifiers As New DataSet

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

If I watch the DataSet variables they have two tables in each of them until
the page_load event ends (with End Sub) and as soon as the button_click
event begins they empty out. I see the same with any strings I declare in
the same manner, the string s become nothing.

Below is my entire button_click event. If I but a breakpoint on the If
statement, the variables still exist but have been cleared at that point.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim modDCI As String

If RadioButtonList1.SelectedIndex <> -1 Then

modDCI =
dsModifiers.Tables(1).Rows.Item(RadioButtonList1.SelectedIndex()).Item("depa
rtment") & _

dsModifiers.Tables(1).Rows.Item(RadioButtonList1.SelectedIndex()).Item("cate
gory") & _

dsModifiers.Tables(1).Rows.Item(RadioButtonList1.SelectedIndex()).Item("item
")

End If

wwSales.ItemSelected(dsItem, Convert.ToInt16(Qty.Text()), modDCI)

End Sub
 
Did you do this?

Protected dsItem As New DataSet, dsModifiers As New DataSet

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
daItems.Fill(dsItem)
daModifiers.Fill(dsModifiers)
End If
End Sub
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
...
End Sub

On the first request of this page, this code will fill the two datasets. The
user will then see the page. The user will click Button1, which will cause a
postback. This time around, the two DataSets will be constructed via "New",
but they will not be filled (Not Page.IsPostBack). When Button1_Click is
called later, the datasets are, of course, not filled.

It's necessary to keep in mind that the page the user sees and the page
which responds to the user clicking the button are two separate instances of
the same class. Members initialized in one instance have no relation to
members initialized in a second instance.
 
Yep. Rookie mistake I guess. I was just realizing it as you must have been
posting.
It seems I've also found myself in a catch 22 of sorts as well.

I have a RadioButtonList on this page that I need to know the selected value
of for that click event. I think I'm hitting the same issue you just pointed
out with that and it always make me end up with a value of -1 for that
control's SelectedIndex property.
How do you typically work around that?

THANKS!
 
Actually John, I got it by assigning the SelectedIndex to a protected
variable. Thanks again for pointing me in the right direction!

Matt
 
MattB said:
Actually John, I got it by assigning the SelectedIndex to a protected
variable. Thanks again for pointing me in the right direction!

Glad you've got it working, Matt.

BTW, you might want to look up the meaning of Protected vs. Private. You'll
want to use Protected when you have something in code-behind which you want
to reference on the .aspx page. Otherwise, Private is adequate.
 
Back
Top