0

I'm doing a jquery ajax call to my server side c#. The c# code fills a datatable then sends the data to the aspx page using a JavaScriptSerializer: The datatable is initialized as a public:

public partial class _Default : System.Web.UI.Page
   {
      DataTable myDataTable = new DataTable();

      protected void Page_Load(object sender, EventArgs e)
         {
            if (!Page.IsPostBack)
            {
               //SqlDataReader builds loads data into dataTable...
               HttpContext.Current.Response.Write(serializer(dataTable));
            }

The aspx page finally creates a table from the data by posting it back to the aspx page. My next task is to sort the rows ascending/descending when the table heading is clicked (using another ajax call).

I'd like to perform a sort operation similar to the accepted answer in this question: Sorting rows in a data table

Will the datatable from the initial ajax call still be in memory? Thank you.

Community
  • 1
  • 1
T3.0
  • 446
  • 1
  • 6
  • 21

1 Answers1

1

No, the dataTable will not be in memoty anymore. Every time you create a request a new instance of _Default will be created. After the request has been handled, it will be destroyed.

If you want the DataTable to remain in memory, you should save it in the Cache or the Application object (HttpContext.Current.Cache / HttpContext.Current.Application).

Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • Thanks Kenneth, I found this out during some testing. Some testing that took me about as long as I spent preparing the question ;-). What I ended up doing is storing the table in a session variable: `Session["dtSession"] = dataTableName;` seems to work ok so far except in my debug mode I notice if I walk away for 10-15 minutes the variable goes MIA – T3.0 Aug 19 '13 at 17:15
  • Yes, that's because of the session timeout. You will get this anyway because after a while (standard 20 minutes) the app pool recycles and you will lose all state. In order to be safe you need to do a null check and load it if it's not loaded – Kenneth Aug 19 '13 at 19:30