title: How to serialize a ViewState objects
body: <p>I have some functions that query a database and puts the result into a gridview. It also sets a ViewState variable that contains the results of the query. When I postback, I have a load function that loads the gridview from the last results if any. </p>
  
  <p>The Problem I am having is that I am getting an error that basically says that the viewstate object needs to be serialized. From what I have I am not sure how to do this. </p>
  
  <blockquote>
    <p>Error: Sys.WebForms.PageRequestManagerServerErrorException: Error
    serializing value
    'System.Collections.Generic.List<code>1[&lt;&gt;f__AnonymousType17</code>2[System.Int32,System.String]]'
    of type
    'System.Collections.Generic.List<code>1[[&lt;&gt;f__AnonymousType17</code>2[[System.Int32,
    mscorlib, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
    App_Web_rjb524gi, Version=0.0.0.0, Culture=neutral,
    PublicKeyToken=null]].'</p>
  </blockquote>
  
  <p>My code so far:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Runtime.Serialization;
  
  public partial class PlayersManagement : System.Web.UI.Page
  { 
  
  protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          LoadData();
      }
  }
  
  public void LoadData()
  {
  
      if (ViewState["CurrentGridView"] != null || ViewState["CurrentGridView"] == "")
      {
          GridViewPlayers.DataSource = ViewState["CurrentGridView"];
          GridViewPlayers.DataBind();
      }
      else
      {
  
          DBModel.DBEntities context = new  DBModel.DBEntities();
  
          var players= (from f in context.Players
                          where f.isDeleted == false
                          select new
                          {
                              f.PlayerId,
                              f.PlayerName
  
                          }).ToList();
  
          GridViewPlayers.DataSource = players;
          GridViewPlayers.DataBind();
      }
  
      ViewState["Sort"] = 0;
  
  }
  
  
  /**
     * This method is for advance search functionality 
     * 
     */
  protected void btnSearch_Click(object sender, EventArgs e)
  {
  
      // If the search textbox is not empty
      if (txtSearch.Text.Trim() != "")
      {
          // Call to Entity Model Framework
         DBModel.DBEntities context = new DBModel.DBEntities();
  
          //Querying the Players table 
          var search = (from s in context.Players
                        where s.PlayerName.Contains(txtSearch.Text.Trim())
                        select new
                        {
                            s.PlayerId,
                            s.PlayerName
  
                        }).ToList();
  
  
          if (search.Count != 0)
          {
  
              noResults.Visible = false;
  
              GridViewPlayers.DataSource = search;//Connecting query to the datasource Gridview
  
              ViewState["CurrentGridView"] = search; // &lt;---- Error cause here
  
              GridViewPlayers.DataBind();  //Binding Gridview
          }
          else
          {
              noResults.Visible = true;
              noResults.Text = "This '" + txtSearch.Text + "' Query Returned No Results";
  
              txtSearch.Text = "";
  
          }
  
  
      }
  
  }
  
  
   protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
  {
  
      //Label2.Text = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
       DBModel.DBEntities context = new  DBModel.DBEntities();
  
      var players = (from b in context.Players
                   where b.isDeleted == false
                   select b);
  
      DataTable gridviewTable = players.CopyToDataTable();
  
      gridviewTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
  
      GridViewPlayers.DataSource = gridviewTable;
  
      ViewState["CurrentGridView"] = gridviewTable; &lt;--- Also causes error
  
      GridViewPlayers.DataBind();
  
  }
  
  
  private string ConvertSortDirectionToSql(SortDirection sortDirection)
  {
      string newSortDirection = String.Empty;
  
      int sort = (ViewState["Sort"] == null) ? 0 : (int)ViewState["Sort"];
      switch (sort)
      {
          case 0:
              newSortDirection = "ASC";
              ViewState["Sort"] = 1;
              break;
  
          case 1:
              newSortDirection = "DESC";
              ViewState["Sort"] = 0;
              break;
      }
  
      return newSortDirection;
  }
  
  protected void GridViewPlayers_RowEditing(object sender, GridViewEditEventArgs e)
  {
      GridViewPlayers.EditIndex = e.NewEditIndex;
      LoadData();
  }
  
  
  protected void GridViewPlayers_CancelEditRow(object sender, GridViewCancelEditEventArgs e)
  {
      GridViewPlayers.EditIndex = -1;
      LoadData();
  }
  </code></pre>