Custom Datagrid Serialization Error's, please help

Discussion in 'ASP .Net Datagrid Control' started by Big Dave, Sep 29, 2004.

  1. Big Dave

    Big Dave Guest

    Good morning. I'm new to .net and object oriented programming, and
    I'm having a problem that I believe resides on the .net side, but I
    can't find a way to correct it within a custom object.

    Here's what I'm trying to do. I've built a custom datagrid abstract
    object that I will inherit from in custom datagrids built for each
    logical business entity within my app. I've marked both the abstract
    class and the implementation as <Serializable()>. The datagrid still
    does not maintain it's state. I tried manually adding it to viewstate
    within my aspx page, and I get the following error:

    The type 'System.Web.UI.WebControls.DataGrid' must be marked as
    Serializable or have a TypeConverter other than ReferenceConverter to
    be put in viewstate.
    Description: An unhandled exception occurred during the execution of
    the current web request. Please review the stack trace for more
    information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: The type
    'System.Web.UI.WebControls.DataGrid' must be marked as Serializable or
    have a TypeConverter other than ReferenceConverter to be put in
    viewstate.

    Source Error:

    An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of
    the exception can be identified using the exception stack trace below.

    Stack Trace:


    [HttpException (0x80004005): The type
    'System.Web.UI.WebControls.DataGrid' must be marked as Serializable or
    have a TypeConverter other than ReferenceConverter to be put in
    viewstate.]
    System.Web.UI.LosFormatter.SerializeValue(TextWriter output, Object
    value)
    System.Web.UI.LosFormatter.SerializeValue(TextWriter output, Object
    value)
    System.Web.UI.LosFormatter.SerializeValue(TextWriter output, Object
    value)
    System.Web.UI.LosFormatter.SerializeInternal(TextWriter output,
    Object value)
    System.Web.UI.LosFormatter.Serialize(TextWriter output, Object
    value)
    System.Web.UI.LosFormatter.EstimateSize(Object obj)
    System.Web.UI.Control.BuildProfileTree(String parentId, Boolean
    calcViewState)
    System.Web.UI.Page.ProcessRequestMain()

    I've looked everywhere I can think of, and I can't get it to work. I
    even tried changing the code to just have a function that returns a
    standard data grid set with all the columns/properties I want, so it's
    no longer a custom server control, just a regular datagrid, and I'm
    still getting the same error. Examples of the code with that change
    are posted below. Does anybody know what I'm doing wrong? Any help
    would be greatly appreciated! Sorry for such a long post.

    NEW CODE USING RETURNING STANDARD DATAGRID:

    Imports System.Data.OleDb
    Imports poker_system.StoredProcedures
    Namespace CommandObjects
    Public Class SessionDA
    Inherits CommandObjects.DataAccessAbstract
    Public Sub New()
    MyBase.new()
    End Sub
    #Region " Command Objects "
    'code removed to keep this post a little smaller, i don't think this
    has anything to do with the issue
    #End Region


    #Region " Poker Session Methods "
    Public Function GetDataAdapter() As OleDbDataAdapter
    Dim da As New OleDbDataAdapter
    da.SelectCommand = Me.GetSelectAllCommand
    Return da
    End Function

    Public Function GetDataSet() As DataSet
    Dim SessionDS As New PokerSessionDS 'my typed dataset

    Dim BetTypeDA As New OleDbDataAdapter
    BetTypeDA = GetBetTypeDA()
    BetTypeDA.Fill(SessionDS.BetType)


    Dim SessionDA As New OleDbDataAdapter
    SessionDA = GetDataAdapter()
    SessionDA.Fill(SessionDS.poker_session)
    Return SessionDS
    End Function

    Public Function GetSessionDataGrid() As DataGrid
    Dim dg As New PokerSessionDG
    dg.LoadGrid(Me.GetDataSet)
    Return dg.GetDataGrid
    End Function
    #End Region

    'code removed to keep this post a little smaller, i don't
    think this has anything to do with the issue
    #Region " Bet Type Methods "
    'code removed to keep this post a little smaller, i don't
    think this has anything to do with the issue
    #End Region
    End Class
    End Namespace


    CUSTOM OBJECT THAT RETURNS THE DATAGRID
    Imports System.ComponentModel
    Imports System.Web.UI


    <DefaultProperty("Text"), Serializable(),
    TypeConverter(GetType(System.ComponentModel.ComponentConverter)),
    ToolboxData("<{0}:pokerSessionDG runat=server></{0}:pokerSessionDG>")>
    _
    Public Class PokerSessionDG
    Inherits WebControl

    Private dg As DataGrid
    Private _sessionDataSet As PokerSessionDS

    Public ReadOnly Property SessionDataGrid() As DataGrid
    Get
    Return dg
    End Get
    End Property
    Public Property SessionDataSet() As PokerSessionDS
    Get
    Return _sessionDataSet
    End Get
    Set(ByVal Value As PokerSessionDS)
    _sessionDataSet = Value
    End Set
    End Property

    Public Function GetDataGrid() As DataGrid
    Return dg
    End Function

    Public Sub LoadGrid(ByVal ds As DataSet)
    Me._sessionDataSet = ds
    dg.DataSource = Me._sessionDataSet.poker_session
    dg.AutoGenerateColumns = False
    dg.EnableViewState = True
    dg.Columns.Add(GetEditColumn())
    dg.Columns.Add(GetSessionId())
    dg.Columns.Add(Me.GetSessionDt())
    Me.Controls.Add(dg)
    End Sub
    Public Sub New()
    dg = New DataGrid
    End Sub
    Private Function GetSessionDt() As BoundColumn
    Dim bc As New BoundColumn
    bc.DataField =
    Me._sessionDataSet.poker_session.session_dtColumn.ToString
    bc.HeaderText = "Date Played"
    Return bc
    End Function
    Private Function GetSessionId() As BoundColumn
    Dim bc As New BoundColumn
    bc.DataField =
    Me._sessionDataSet.poker_session.session_idColumn.ToString
    bc.HeaderText = "Session Id"
    Return bc
    End Function
    Private Function GetEditColumn() As EditCommandColumn
    Dim ec As New EditCommandColumn
    ec.CancelText = "Cancel"
    ec.EditText = "Edit"
    ec.UpdateText = "Update"
    Return ec
    End Function

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Me.RenderChildren(output)
    End Sub

    Public Sub BindGrid()
    dg.DataBind()
    End Sub

    End Class


    ASPX PAGE SUBROUTINE CALLING THE PREVIOUS CODE
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    Dim PokerSessionDA As New CommandObjects.SessionDA
    SessionDataGrid = PokerSessionDA.GetSessionDataGrid

    If Me.IsPostBack = False Then
    SessionDataGrid.DataBind()
    Viewstate.Add("datagrid", SessionDataGrid)
    Else
    SessionDataGrid = ViewState("Datagrid")
    End If




    End Sub

    OLD CODE, THE WAY I WOULD HAVE LIKED TO DO IT, BUT I CHANGED IT WHEN
    THIS DIDN'T WORK

    Here is an example of the custom grid I built that also wouldn't
    serialize:

    Imports System.ComponentModel
    Imports System.Web.UI


    <DefaultProperty("Text"), Serializable(),
    ToolboxData("<{0}:pokerSessionDG runat=server></{0}:pokerSessionDG>")>
    _
    Public Class PokerSessionDG

    Inherits CustomControls.CustomDataGrid.CustDataGrid

    Private dg As DataGrid
    Private _sessionDataSet As PokerSessionDS

    Public Property SessionDataSet() As PokerSessionDS
    Get
    Return _sessionDataSet
    End Get
    Set(ByVal Value As PokerSessionDS)
    _sessionDataSet = Value
    End Set
    End Property

    Public Overrides Sub LoadGrid(ByVal ds As DataSet)
    Me._sessionDataSet = ds
    MyBase.DataSource = Me._sessionDataSet.poker_session
    MyBase.AutoGenerateColumns = False
    MyBase.EnableViewState = True
    MyBase.Columns.Add(GetEditColumn())
    MyBase.Columns.Add(GetSessionId())
    MyBase.Columns.Add(Me.GetSessionDt())
    MyBase.Controls.Add(dg)
    End Sub
    Public Sub New()
    MyBase.new()
    End Sub
    Private Function GetSessionDt() As BoundColumn
    Dim bc As New BoundColumn
    bc.DataField =
    Me._sessionDataSet.poker_session.session_dtColumn.ToString
    bc.HeaderText = "Date Played"
    Return bc
    End Function
    Private Function GetSessionId() As BoundColumn
    Dim bc As New BoundColumn
    bc.DataField =
    Me._sessionDataSet.poker_session.session_idColumn.ToString
    bc.HeaderText = "Session Id"
    Return bc
    End Function
    Private Function GetEditColumn() As EditCommandColumn
    Dim ec As New EditCommandColumn
    ec.CancelText = "Cancel"
    ec.EditText = "Edit"
    ec.UpdateText = "Update"
    Return ec
    End Function

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    MyBase.Render(output)
    End Sub

    Public Overrides Sub BindGrid()
    MyBase.DataBind()
    End Sub

    End Class

    And here is the abstract class that it inherits

    Imports System.ComponentModel
    Imports System.Web.UI
    Namespace CustomDataGrid

    <DefaultProperty("Text"), Serializable(),
    ToolboxData("<{0}:CustDataGrid runat=server></{0}:CustDataGrid>")> _
    Public MustInherit Class CustDataGrid
    Inherits System.Web.UI.WebControls.DataGrid

    Private dg As New WebControls.DataGrid

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    MyBase.Render(output)
    End Sub

    Public MustOverride Sub BindGrid()

    Public MustOverride Sub LoadGrid(ByVal ds As DataSet)

    Public Sub New()
    MyBase.CellPadding = 3
    End Sub

    Protected Overrides Sub CreateControlHierarchy(ByVal
    useDataSource As Boolean)
    MyBase.CreateControlHierarchy(useDataSource)
    End Sub

    Protected Overrides Sub PrepareControlHierarchy()
    MyBase.PrepareControlHierarchy()
    End Sub
    End Class
    End Namespace
     
    Big Dave, Sep 29, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,315
    deadsea
    Jan 2, 2005
  2. KK
    Replies:
    2
    Views:
    735
    Big Brian
    Oct 14, 2003
  3. Replies:
    3
    Views:
    1,088
  4. Dimitri Ognibene
    Replies:
    4
    Views:
    825
    Dimitri Ognibene
    Sep 2, 2006
  5. Ramunas Urbonas
    Replies:
    1
    Views:
    427
    Dino Chiesa [Microsoft]
    Jul 27, 2004
Loading...

Share This Page