[RESOLVED] keep user input available through gridview paging
Hi, I'm trying to cache user's input avail through gridview paging. I receive error System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object." Any thought is appreciated. Thank you in advance.
When I walked through CacheData, it seems to cache the data just fine. However, sd is empty.
Code:
Private Sub CacheData()
Dim row As GridViewRow
Dim sd1 As New StringDictionary
Dim count As Integer = 0
For Each row In GridView1.Rows
Dim txtRoomNum As TextBox = CType(row.FindControl("RoomNumber"), TextBox)
Dim lblID As Label = CType(row.FindControl("RetreatantID"), Label) 'a way to identify the row, I use primary key. This would be a template column
sd1.Add(lblID.Text, txtRoomNum.Text)
Next
Dim index As String = GridView1.PageIndex
Cache.Insert("cachedRMAssignValues" & index, sd1)
End Sub
Private Sub setCachedEdit()
Dim sd As StringDictionary = Cache.Get("cachedRMAssignValues" & GridView1.PageIndex)
Dim de As New DictionaryEntry
Dim r As GridViewRow
If sd.Count > 0 Then
For Each de In sd
For Each r In GridView1.Rows
Dim txtRoomNum As TextBox = CType(r.FindControl("RoomNumber"), TextBox)
Dim lblID As Label = CType(r.FindControl("RetreatantID"), Label)
Dim index As String = GridView1.PageIndex
If lblID.Text = de.Key Then
txtRoomNum.Text = de.Value
End If
Next
Next
End If
End Sub
Here's my GridView Paging:
Code:
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
Dim rpData As New clsACData
Dim groupID As String = Session("groupID")
If GridView1.EditIndex <> -1 Then
e.Cancel = True
End If
If (dlSearchOptions.SelectedValue = "recruiter name" Or dlSearchOptions.SelectedValue = "retreatant name" Or dlSearchOptions.SelectedValue = "retreatant not assigned a room" Or dlSearchOptions.SelectedValue = "special needs") Then
CacheData()
GridView1.PageIndex = e.NewPageIndex
GridView1.DataSource = SortDataTable(rpData.RetreatantsForRMAssign(getSearchOptions(), getInput(), groupID), True)
GridView1.DataBind()
setCachedEdit()
ElseIf (dlSearchOptions.SelectedValue = "registration status") Then
CacheData()
GridView1.DataSource = SortDataTable(rpData.RetreatantsForRMAssign(getSearchOptions(), getRegStatus(), groupID), True)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
setCachedEdit()
ElseIf (dlSearchOptions.SelectedValue = "building") Then
CacheData()
GridView1.DataSource = SortDataTable(rpData.RetreatantsForRMAssign(getSearchOptions(), getRegBuilding(), groupID), True)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
setCachedEdit()
End If
Re: keep user input available through gridview paging
Went through your code, it looks like it should work, obviously. It's a little hard to see why this is happening, but on what line is the error thrown, for what object?
Re: keep user input available through gridview paging
I totally agree that it should work! However, it fails on If sd.Count > 0 Then
in setCachedEdit(). The error msg is System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." I think at that point, sd > 0 is not true, sd is empty.
The funny thing is, when I change Dim index As String = GridView1.PageIndex + 1 in CacheData(), it cached whatever values that I assigned in the previous time I compiled. Then, I go back and changed the above statement back to original (Dim index As String = GridView1.PageIndex in CacheData()), everything is working fine after that, all values cached when paging!!! I don't know what's going on, obviously, it's driving me nuts...Any idea?
Re: keep user input available through gridview paging
When debugging, is GridView1.PageIndex = e.NewPageIndex anyways? Or are they different values before the statement is executed?
Also, this statement is somewhat worrying
Quote:
The funny thing is, when I change Dim index As String = GridView1.PageIndex + 1 in CacheData(), it cached whatever values that I assigned in the previous time I compiled.
You're values from the previous compilation are remembered?
Re: keep user input available through gridview paging
i think its something to do with your logic as the page event occurs...
Try adding a parameter to your sub and use the grids previous index before the event fires off.
Code:
currentGridIndex Being e.NewPageIndex? or e.index? (off of my memory)
Private Sub setCachedEdit(currentGridIndex as integer)
Dim sd As StringDictionary = Cache.Get("cachedRMAssignValues" & currentGridIndex)
'OR you COULD do this to prevent the crash:
Dim sd As StringDictionary
if not Cache.Get("cachedRMAssignValues" & currentGridIndex) Is Nothing then
sd =Cache.Get("cachedRMAssignValues" & currentGridIndex)
If sd.Count > 0 Then .....
' rest of ur code
end if
end if
End Sub
USING setCachedEdit(e.NewPageIndex) when u call setCachedEdit().
Re: keep user input available through gridview paging
Mendhak: Yes, my values from the previous compilation are remembered. I find it very odd myself. How? I don't know. That's why it's driving me nuts.
Eclipsyo: I agree that some kind of logic is off set as the page event occurs. Although I don't know why yet, your idea seems to work. Thank you very much!