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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving a collection in viewstate

Status
Not open for further replies.

jerasi

Programmer
Jun 19, 2000
141
CA
Hello,
I would like to save a class collection into the viewstate but I get the following error:
"The type 'WebApplication2.Users' must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in viewstate"

I have a "Users" which is a collection of "User" classes. Is there an easy way to save it into the viewstate?
Thank you.


Let me put it this way:
While StressLevel Is High
Productivity -= 1
Marriage = Nothing
End While
 
You have to mark you classes as Serializable

<Serializable()> _
Public Class Claim
'Class definition
End Class

Any class you want to have in view state must be Serializable. Therefore a StringDictionary is not Serializable but a ArrayList is. You'll see this in MSDN

[Visual Basic]
<Serializable>
Public Class ArrayList
Implements IList, ICollection, IEnumerable, ICloneable
 
I tried that but no luck. Here's my class definition

<Serializable()> Public Class Users
Implements IEnumerable

I'm trying to add it to the ViewState like this:
dim m_Users as New Users
viewstate("m_Users") = m_Users

I still get the error:
"The type 'WebApplication2.Users' must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in viewstate."

Let me put it this way:
While StressLevel Is High
Productivity -= 1
Marriage = Nothing
End While
 
Can you provide the whole users definition. Everything in the class definition must be serializable.
 
Here's the whole class. Thank you for your time.


<Serializable()> Public Class Users
Implements IEnumerable

Dim m_Collection As Collection

Public Sub New()
m_Collection = New Collection
End Sub

Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return m_Collection.GetEnumerator
End Function

Public Function Add(ByVal oUser As User) As Boolean
Dim bAdded As Boolean = True
Try
Dim oCurUser As User
For Each oCurUser In m_Collection
If oCurUser Is oUser Then
bAdded = False
Exit For
End If
Next
If bAdded Then
m_Collection.Add(oUser)
End If
Catch ex As Exception
bAdded = False
End Try
Return bAdded
End Function

Public Function GetUserByUserId(ByVal iUserID As Integer) As User
Dim oUser As User, bMatchFound As Boolean = False
For Each oUser In m_Collection
If oUser.iUserID = iUserID Then
bMatchFound = True
Exit For
End If
Next
If Not bMatchFound Then
oUser = Nothing
End If
Return oUser
End Function

End Class

Let me put it this way:
While StressLevel Is High
Productivity -= 1
Marriage = Nothing
End While
 
Dim m_Collection As Collection" as in Microsoft.VisualBasic.Collection

UHG...

Get that out of there NOW!!!!!. VB is ok for somethings but can beat you up. If this isn't caussing the problem it will cause you a problem sometime in the future.

maybe you could do this instead.

Good luck
 
I do this all the time to store collections of objects. Here is the skeleton of what I always start with:

Code:
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Imports System.Diagnostics
<Serializable()> _
Public Class [class name goes here]
   Public ItemList As New ArrayList
   Public Shared Sub Main()
      ' Creates and initializes a new ArrayList.
   End Sub

   Public Sub AddItem(ByVal obj)

      ItemList.Add(obj)

   End Sub


End Class

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top