1

i am tired wondering the issue for this problem. have read so many blogs and forums for this but i am not able to find out the problem.

I am using "SQLServer" mode to store session.

Everything is working fine. But whenever i use search function in my website, it throws the below error:

"Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode."

I am assuming that this is because of the paging code i have used on that page. That code is as below:

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string query = "select xxxxqueryxxxx";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        try
        {
            using (con)
            {
                con.Open();
                da.Fill(ds);
            }
        }
        catch
        {
            ds = null;
        }
        finally
        {
            if (ds != null)
            {
                CustomPaging page = new CustomPaging();
                DataTable dt = ds.Tables[0];
                page.PageSize = 10;
                page.DataSource = dt;
                page.CurrentPageIndex = 0;
                no = 1;
                Session["DT"] = dt;
                Session["page"] = page;
                bindData(page, dt);

                //set these properties for multi columns in datalist
                DataList2.RepeatColumns = 1;
                DataList2.RepeatDirection = RepeatDirection.Horizontal;
            }
        }
    }
}
void bindData(CustomPaging page, DataTable dt)
{
    try
    {
        DataList2.DataSource = page.DoPaging;
        DataList2.DataBind();
        //DataList2.DataSource = SqlDataSource1;
        //DataList2.DataBind();
        lbtnPre.Enabled = !page.IsFirstPage; //Enable / Disable Navigation Button
       // lbtnPre.CssClass = "disabledbtn";
        lbtnNext.Enabled = !page.IsLastPage;
        //lbtnNext.CssClass = "disabledbtn";
        lblStatus.Text = NavigationIndicator(); //Build Navigation Indicator 
        //for creating page index
        DataTable dt1 = new DataTable();
        dt1.Columns.Add("PageIndex");
        dt1.Columns.Add("PageText");

        for (int i = 0; i < page.PageCount; i++)
        {
            DataRow dr = dt1.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt1.Rows.Add(dr);
        }
        dlPaging.DataSource = dt1;
        dlPaging.DataBind();
        dlPaging.RepeatColumns = 10;
        dlPaging.RepeatDirection = RepeatDirection.Horizontal;
    }
    catch (Exception)
    {
    }
    finally
    {
        page = null;
    }
}
string NavigationIndicator()
{
    string str = string.Empty;     //Page x Of Y
    str = Convert.ToString(((CustomPaging)Session["page"]).CurrentPageIndex + 1) + " of " + ((CustomPaging)Session["PAGE"]).PageCount.ToString() + " Page(s) found";
    return str;
}
protected void lbtnPre_Click(object sender, EventArgs e)
{
    int pageIndex = ((CustomPaging)Session["page"]).CurrentPageIndex;
    if (!((CustomPaging)Session["page"]).IsFirstPage)
        //Decrements the pageIndex by 1 (Move to Previous page)
        ((CustomPaging)Session["page"]).CurrentPageIndex -= 1;
    else
        ((CustomPaging)Session["page"]).CurrentPageIndex = pageIndex;

    //Binds the DataList with new pageIndex
    bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
    int pageIndex = ((CustomPaging)Session["page"]).CurrentPageIndex;
    if (!((CustomPaging)Session["page"]).IsLastPage)
        //Increments the pageIndex by 1 (Move to Next page)
        ((CustomPaging)Session["page"]).CurrentPageIndex += 1;
    else
        ((CustomPaging)Session["page"]).CurrentPageIndex = pageIndex;

    //Binds the DataList with new pageIndex
    bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));
}
protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect("Default2.aspx?partnumber=" + DataList2.DataKeyField[DataList2.SelectedIndex].ToString());
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        no = int.Parse(e.CommandArgument.ToString()) + 1;
        ((CustomPaging)Session["page"]).CurrentPageIndex = int.Parse(e.CommandArgument.ToString());
        //Binds the DataList with new pageIndex
        bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));

    }
}
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
    LinkButton btn = (LinkButton)e.Item.FindControl("lnkbtnPaging");

    if (btn.Text == no.ToString())
    {
        btn.ForeColor = System.Drawing.Color.Maroon;
        btn.Font.Underline = false;
    }
    else
    {
        btn.ForeColor = System.Drawing.Color.DarkCyan;
        btn.Font.Underline = false;
    }
}

I just want to know what the problem is in the coding and "how to serialize the session"? What shold i do to improve the coding?

Any help will be appreciated.

Thank You

Jack
  • 363
  • 1
  • 4
  • 14
  • IS IT THE ISSUE BECAUSE OF I AM USING LOT OF SESSIONS TO STORE IN PAGING? SHOULD I USE ANOTHER CODE FOR PAGING? OR SHOULD I MODIFY ANY HOW TO MAKE ALL THE SESSION OBJECTS SERIALIZABLE? – Jack Mar 13 '13 at 10:28
  • 3
    STOP USING CAPSLOCK WE ARE NOT BLIND – Eugene Pavlov Mar 13 '13 at 10:47
  • try to use search next time before posting duplicate questions. please check this: http://stackoverflow.com/questions/10230880/serializing-session-state-in-asp-net – Eugene Pavlov Mar 13 '13 at 10:53
  • thank you for the comment but i have already seen this. and its my own choice weather to use caps lock or not but thanks for the advice. it would be great if you stop giving the advice and start giving the solution because that's why you are here for to help other who are new in this field. – Jack Mar 13 '13 at 10:59
  • FYI IT IS HARD TO READ ANY TEXT IN CAPSLOCK for the solution, maybe instead of passing datatable in session from page to page, try to pass simple ID and retrieve data you need by this id directly on other page – Eugene Pavlov Mar 13 '13 at 11:09
  • thanks i can try that, will that help? – Jack Mar 13 '13 at 11:43

1 Answers1

1

I guess your custom paging control is not serializable. This must be the cause of the issue.

Anyway, storing a control in session is not a good idea. Just store the few serializable properties which enable to rebuild the control (PageSize and CurrentPageIndex), or pass them in query string for example.

You could also use ViewState if you can

About storing the DataTable in Session, this might be a really bad idea if you have a lot of data and many connected users.

jbl
  • 15,179
  • 3
  • 34
  • 101
  • Storing int values in the session is basic session handling. About viewstate, have a look at http://msdn.microsoft.com/en-us/library/ms227551%28v=vs.85%29.aspx – jbl Mar 13 '13 at 13:01