mark fisher

mark fisher

  • NA
  • 29
  • 2.9k

Put DataList into Session Object

Jun 2 2013 5:24 PM
Hi I have a DataList that is getting rows from a table and filling my DataList. When my user leaves my page, I want to hold this DataList in Session and show it again if/when the user returns to the page. I created a function that creates a new DataTable, and stores my DataList dataTable in, then I assigned it to a Session object. But I cannot get it to show the list again when the user revisits the page. I checked the debugger and my columns are there in my second dataTablebut, It is not displaying. Here is my code. Can someone make sure it is functional. Thank you very much.

//my method to create a new DataTable and store the DataList rows.
protected DataTable CreateHeathCenterAddressTable(DataTable tableSchema)
    {
        DataTable tableAddressData = new DataTable();

        foreach (DataColumn dc in tableSchema.Columns)
        {
            tableAddressData.Columns.Add(dc.ColumnName,
                  dc.DataType);
        }
        return tableAddressData;

    }

//then I'm calling the original data from my database and passing that DataTable into my function.

SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(prcLocationListByGeography);
            DataTable dtViewResult = new DataTable();
            SQLDataAdapter.Fill(dtViewResult);
            healthCenters.DataSource = dtViewResult;
            healthCenters.DataBind();
            SQLDataAdapter.Dispose();
            con.Close();
            DataTable tableAddressItems = CreateHeathCenterAddressTable(dtViewResult);//here I'm calling my method and passing in the dtViewResult
            Session["tableAddressItems"] = tableAddressItems;

        }

//on the page load I'm doing this
if(Session["tableAddressItems"] != null)
{
    healthCenters.DataSource = Session["tableAddressItems"];
    healthCenters.DataBind();

}
//I set a trace on the page, and my DataTable is getting held in Session??


Answers (2)