locked
Class variables RRS feed

  • Question

  • User1471857856 posted

     I am creataing an ASP.Net page with VB.Net codebehind.  I am using a class variable to hold data (named ods see below) from a SQL Server query so I do not have to keep going back out to the server.  The class variable gets loaded ok and I populate a grid data with this dataset.  However when I need to go back in and reference the dataset again, I get an error and I come to find out my data set no longer holds the data.  Why? 

    Here is my declaration in my codebehind

     

    Partial Public Class LotTrackingMain Inherits System.Web.UI.Page

     

    Private con As SqlConnection = Nothing

    Private ods As New DataSet

    Thursday, October 1, 2009 8:12 AM

Answers

  • User-952121411 posted

     As mentioned before the web is "Stateless".  You may ask, "Well what does that mean?"  It means that the client (the user and thier browser) and the server (the ASP.NET server) have no permenant historical relationship, and that each time a request is made from the client the server knows nothing about any previous requests.  Each new request is handles by the server not know of any pervious transaction and deals with the new information based on the current information provide.

    What does this equate to for you?  Well if you declare a variable like a DataSet and give it value, the value will not persist any postabcks from the client.  Now to get around the 'Stateless' architecture a bunch of methodologies have come about to persist data as you need to do.  Several of these methods were listed in the last post, so I am not going to repeat them here.  I will comment though that each method has a plus and a minus and must be understood well in order to make a good decision on which to use.  For example, while ViewState increases page size and is not very secure (because it is in the page's source), it still has its place.  A small variable value in a hidden field that only needs to persist postbacks for a single page may find a nice home in ViewState where as Session Values are global to the entire app.  For an entire DataSet as you have, ViewState is not a good option.

    I reccomend you place your DataSet into a Session variable and then re-access it after a post back to get your data back out of the Session Variable.  Below is some sample code:

    1st the code to place the DataSet into a Session variable:

    'Declare a DataSet
    Dim ods As New DataSet
    'Do some code to populate the DataSet...
    
    'Place the populated DataSet into a Session variable that can be accessed upon postback
    Session.Add("MyDataSet", ods)

     

    2nd, the code to get the populated DataSet back out of the session varibale:

    'A postback has occured and we are in a new method; declare a DataSet
    Dim odsAfterPostBack As DataSet
    'Pull the DataSet back out of session to be used again; it will contain all of the data in the dataset as expected
    odsAfterPostBack = DirectCast(Session("MyDataSet"), DataSet)


    ASP.NET Session State Overview:

    http://msdn.microsoft.com/en-us/library/ms178581.aspx

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 1, 2009 11:15 AM
  • User1471857856 posted

     Yes, Thanks this does help, being the dataset I am creating is not very large, putting it into a session variable should not be a problem. 

    Thanks again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 1, 2009 11:31 AM

All replies

  • User-837620913 posted

    ASP.NET does not hold things "in memory".  The web is a stateless medium.  If you click a button then ASP.NET needs to either:

    1.  Recreate the data by going to the database

    2.  Recreate the data by reading it from viewstate (NOT a good idea)

    3.  Recreate the data by reading it from Session state

    The downside to #1 is increased load on the database.  The downside to #2 is increased page size and slower responsiveness to the end user.  The downside to #3 is your app is less scalable because it uses more memory, and you will run into problems if you ever try to scale out to multiple web servers.

    Check out this intro to ASP.NET State:

    http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx

    Thursday, October 1, 2009 8:38 AM
  • User-952121411 posted

     As mentioned before the web is "Stateless".  You may ask, "Well what does that mean?"  It means that the client (the user and thier browser) and the server (the ASP.NET server) have no permenant historical relationship, and that each time a request is made from the client the server knows nothing about any previous requests.  Each new request is handles by the server not know of any pervious transaction and deals with the new information based on the current information provide.

    What does this equate to for you?  Well if you declare a variable like a DataSet and give it value, the value will not persist any postabcks from the client.  Now to get around the 'Stateless' architecture a bunch of methodologies have come about to persist data as you need to do.  Several of these methods were listed in the last post, so I am not going to repeat them here.  I will comment though that each method has a plus and a minus and must be understood well in order to make a good decision on which to use.  For example, while ViewState increases page size and is not very secure (because it is in the page's source), it still has its place.  A small variable value in a hidden field that only needs to persist postbacks for a single page may find a nice home in ViewState where as Session Values are global to the entire app.  For an entire DataSet as you have, ViewState is not a good option.

    I reccomend you place your DataSet into a Session variable and then re-access it after a post back to get your data back out of the Session Variable.  Below is some sample code:

    1st the code to place the DataSet into a Session variable:

    'Declare a DataSet
    Dim ods As New DataSet
    'Do some code to populate the DataSet...
    
    'Place the populated DataSet into a Session variable that can be accessed upon postback
    Session.Add("MyDataSet", ods)

     

    2nd, the code to get the populated DataSet back out of the session varibale:

    'A postback has occured and we are in a new method; declare a DataSet
    Dim odsAfterPostBack As DataSet
    'Pull the DataSet back out of session to be used again; it will contain all of the data in the dataset as expected
    odsAfterPostBack = DirectCast(Session("MyDataSet"), DataSet)


    ASP.NET Session State Overview:

    http://msdn.microsoft.com/en-us/library/ms178581.aspx

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 1, 2009 11:15 AM
  • User1471857856 posted

     Yes, Thanks this does help, being the dataset I am creating is not very large, putting it into a session variable should not be a problem. 

    Thanks again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 1, 2009 11:31 AM