InProc vs StateServer

Discussion in 'ASP .Net' started by tshad, Jul 16, 2005.

  1. tshad

    tshad Guest

    I switched my site from InProc to StateServer a while ago which solve a
    problem I was having dropping my session cookies when the worker process
    recycles.

    This has been working well until today.

    I was looking at some sample code and found that it won't work under
    StateServer, but will under InProc.

    The message I get is:

    Unable to serialize the session state. Please note that non-serializable
    objects or MarshalByRef objects are not permitted when session state mode is
    'StateServer' or 'SQLServer'.

    What are the objects I can't use and why not?

    The code I was looking at was :
    ********************************************************************************
    <%@ Page Language="VB" Debug="False" Explicit="True" Buffer="True"%>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <html><head><title>Drilldown Datagrid Searching Within
    Results</title></head>
    <script runat="server" language="VB">
    Sub Page_Load(Source As Object, E As EventArgs)
    Response.BufferOutput = "True"
    If Len(Request("search")) > 0 Or Len(Request("sub")) > 0 Then
    If Len(Request("search")) > 0 Then Session.Clear()
    MyDataGrid.CurrentPageIndex = 0 ' resets the Datagrid to page 1
    BindGrid()
    subsrch.visible = "true"
    Else
    subsrch.visible = "false"
    End If
    End Sub

    Sub BindGrid()
    Dim Source As DataView

    If Len(Request("search")) = 0 Then Source = CType(Session("CachedGrid"),
    DataView)
    If IsNothing(Source) Then
    Dim MyConnection As SqlConnection
    Dim MyCommand As SqlDataAdapter
    Dim RcdCount As integer
    Dim DS As DataSet

    Dim sqlQuery As String = "SELECT emp_id, fname, lname FROM employee where
    (fname like '%" & Request("search") & "%' or lname like '%" &
    Request("search") & "%') Order by emp_id asc"

    Dim strConn As String = "server=(local);uid=sa;pwd=;database=pubs;"
    MyConnection = New SqlConnection(strConn)
    MyCommand = New SqlDataAdapter(sqlQuery, MyConnection)
    DS = new DataSet()
    MyCommand.Fill(DS, "MyDataGrid")
    MyDataGrid.CurrentPageIndex = 0
    Source = DS.Tables(0).DefaultView
    ' Insert Dataset into Session
    Session("CachedGrid") = Source
    End If
    If Len(Request.Form("sub")) > 0 Then
    Session ("Sub") &= " and (fname LIKE '%" & Request.Form("sub") & "%' or
    lname LIKE '%" & Request.Form("sub") & "%') "
    Source.RowFilter = Session ("Sub").SubString(5)
    End If
    If (Source.Count.ToString) >= 1 Then
    MyDataGrid.visible = "true"
    Else
    msg.text = "<br><b>No records found</b>"
    subsrch.visible = "false"
    MyDataGrid.visible = "false"
    End If
    Try
    'Get count from Dataview row count, same as Datagrid row
    count
    count.text = "<b>" & Source.Count.ToString & "
    </b>results found<br>"
    MyDataGrid.DataSource = Source
    MyDataGrid.DataBind()
    Catch e As Exception
    MyDataGrid.CurrentPageIndex = 0 'reset the Datagrid back
    to page 1 on any errors
    End Try
    End Sub

    Sub MyDataGrid_Page(sender As Object, e As
    DataGridPageChangedEventArgs)
    MyDataGrid.CurrentPageIndex = e.NewPageIndex
    BindGrid()
    End Sub
    </script>
    <body bgcolor="#FFFFFF" topmargin="0" marginheight="0"
    onLoad="document.forms[0].search.focus();">
    <center>
    <h3>Datagrid Drilldown Search Example</h3>
    <form method="post">
    <input type="text" name="search">
    <input type="submit" Value="Go">
    </form>
    <form runat="server">
    <span id="subsrch" runat="server">
    Search Within results
    <input type="text" name="sub">
    <input type="submit" Value="Go">
    </span>
    <br><asp:label id="msg" runat="server" />
    <br><asp:label id="count" runat="server" />
    <ASP:DataGrid id="MyDataGrid" runat="server"
    AllowPaging="True" PageSize="10" PagerStyle-Mode="NumericPages"
    PagerStyle-HorizontalAlign="Right"
    PagerStyle-NextPageText="Next" PagerStyle-PrevPageText="Prev"
    OnPageIndexChanged="MyDataGrid_Page"
    BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3"
    CellSpacing="0" Font-Name="Verdana"
    Font-Size="8pt" HeaderStyle-BackColor="#FF8040"
    AlternatingItemStyle-BackColor="#eeeeee" />
    </form>
    </center>
    </body>
    </html>
    ************************************************************************************

    There are some asp.net objects as well as some "input" html. These normally
    work.

    What is the serialization that they are talking about that I can't use?

    Thanks,

    Tom
     
    tshad, Jul 16, 2005
    #1
    1. Advertisements

  2. tshad

    WJ Guest

    To store session state to stateServer and or Sql Server (ASPState), your
    class must be marked as [Serializable]. Otherwise, only InProc can handle
    stuffs like dataGrid. See example below:

    namespace nSession
    {
    [Serializable()]
    public class cSession
    {
    public string YourName;
    public DateTime YourDOB;

    public cSession()
    {
    }
    }
    }
    *********************
    There is a good article about SS, click here:
    http://www.eggheadcafe.com/articles/20021016.asp

    John
     
    WJ, Jul 16, 2005
    #2
    1. Advertisements

  3. tshad

    Robert Smith Guest

    The message I get is:

    Unable to serialize the session state. Please note that non-serializable
    objects or MarshalByRef objects are not permitted when session state mode is
    'StateServer' or 'SQLServer'.




     
    Robert Smith, Jul 16, 2005
    #3
  4. tshad

    tshad Guest

    But this isn't a class.

    It is just a straight ASP.NET page. I don't add anything that says it is
    Serializable, so what is causing this page not to work?

    Tom
     
    tshad, Jul 16, 2005
    #4
  5. tshad

    tshad Guest

    That was the message I got.

    I just don't know why this page gives me the message, when all the other
    pages (about 150) don't.

    Tom
     
    tshad, Jul 16, 2005
    #5
  6. tshad

    WJ Guest

    Trap these statements below to see if its bombed out:

    Dim Source As DataView
    If Len(Request("search")) = 0 Then Source =
    CType(Session("CachedGrid"),DataView)

    If the error happens here then DataView is not serializable ! Keep in mind
    that "Inproc" will work with DataSet, Table, View ,Grid...

    John
     
    WJ, Jul 17, 2005
    #6
  7. tshad

    WJ Guest

    Correction: I was too fast. It should be:

    ' Insert Dataset into Session
    Session("CachedGrid") = Source
    End If

    The above Session "write" statement may have caused the error because the
    Source is DataView, which may not work with State and SQL Servers. But I
    could be wrong. IOW, you must find out where the exception takes place.

    Sorry.

    John

    John
     
    WJ, Jul 17, 2005
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.