locked
Shared variable RRS feed

  • Question

  • User841655146 posted

    Hallo,

    I create a datatable. I want to use the datatable in every Sub of my Page. The table schould not lose the date.

    So I define the datatable as Shared variable.


    Now I have a Problem. The table for User 1 and User 2,3.... are the same. That means, when user1 add a row, user 2 see the row too.

    I think this is due to the variable.


    What can I do? 

     

    Monday, January 11, 2010 10:41 AM

Answers

  • User-126244515 posted

    If the DataTable object isn't too big, you can save it to a Session variable, which means it is accessible for a single session only. http://msdn.microsoft.com/en-us/library/ms178581.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 11, 2010 11:01 AM
  • User-952121411 posted

    So I define the datatable as Shared variable.


    Now I have a Problem. The table for User 1 and User 2,3.... are the same.

     

    Yes, welcome to the world of variable scope and your 1st lesson on why having everything 'Public' is easiest, but definitely not the best.  This is typically the path a beginner programmer takes before learning about how to make scope only as broad as absolutely needed.

    Just to have a reference for scope at a high level to save off for future reference, take a look to the following link:

    Access Levels in Visual Basic:

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

    Now in an ASP.NET website, you are going to have to tackle persistence a little differently then in a thick client application.  Since web apps do not have state, you will need to persist the DataTable in Session.  The best way to do this is to create a new session variable in the Global.asax file like below.  The session is user specific which will solve your 'Shared' issue.  1st create the variable in the Global.asax.vb Session_Start() method:

        Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Fires when the session is started
    
            'Create session variables that will persist data
            Session.Add("UsersDataTable", Nothing)
    
        End Sub


    Next assign the value whenever returned or created like below:

    'Assign session variable from method 'GetUserData()' that returns a DataTable
    Session("UsersDataTable) = GetUserData()


    Lastly, when you need to access the DataTable again later, pull it back out of session and cast it to a DataTable like below:

    Dim dtUser As New DataTable
    dtUser = DirectCast(Session("UsersDataTable), DataTable)


    And that is the basics of using Session to store a Global type variable, but yet keeping it in scope to the current user only.

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 11, 2010 12:17 PM
  • User-1267218547 posted

    Hi zerberos ,

    From your description, I think you want to share some data with all users.

    If yes, you can use the Cache. since the Cache object is based on all users in the Web Application,

    Cache["d"]=datatable; 

    Then if some user inserts any records, you need to disable the original Cache object and retrieve new data for new Cache object.

    http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

    I hope it is helpful to you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2010 3:45 AM
  • User-952121411 posted

    Now I have a Problem. The table for User 1 and User 2,3.... are the same. That means, when user1 add a row, user 2 see the row too.

    I think this is due to the variable.


     zerberos -

     I re-read your original post several times, and it appears that you indicate that the problem is all user's are seeing the same data and you do not want this.  If this is true, then using Session as described in my prior post will be a solution.

    However, if you do want all users to see the datatable and the problem was it could not be seen by everyone, then use the previously provided suggestions of using the Cache or an Application variable.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2010 11:01 AM

All replies

  • User-126244515 posted

    If the DataTable object isn't too big, you can save it to a Session variable, which means it is accessible for a single session only. http://msdn.microsoft.com/en-us/library/ms178581.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 11, 2010 11:01 AM
  • User-952121411 posted

    So I define the datatable as Shared variable.


    Now I have a Problem. The table for User 1 and User 2,3.... are the same.

     

    Yes, welcome to the world of variable scope and your 1st lesson on why having everything 'Public' is easiest, but definitely not the best.  This is typically the path a beginner programmer takes before learning about how to make scope only as broad as absolutely needed.

    Just to have a reference for scope at a high level to save off for future reference, take a look to the following link:

    Access Levels in Visual Basic:

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

    Now in an ASP.NET website, you are going to have to tackle persistence a little differently then in a thick client application.  Since web apps do not have state, you will need to persist the DataTable in Session.  The best way to do this is to create a new session variable in the Global.asax file like below.  The session is user specific which will solve your 'Shared' issue.  1st create the variable in the Global.asax.vb Session_Start() method:

        Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Fires when the session is started
    
            'Create session variables that will persist data
            Session.Add("UsersDataTable", Nothing)
    
        End Sub


    Next assign the value whenever returned or created like below:

    'Assign session variable from method 'GetUserData()' that returns a DataTable
    Session("UsersDataTable) = GetUserData()


    Lastly, when you need to access the DataTable again later, pull it back out of session and cast it to a DataTable like below:

    Dim dtUser As New DataTable
    dtUser = DirectCast(Session("UsersDataTable), DataTable)


    And that is the basics of using Session to store a Global type variable, but yet keeping it in scope to the current user only.

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 11, 2010 12:17 PM
  • User771898575 posted

    Shared variable mans a Application Level Variable.
    You Shoul Try saving the information in the Session Variable.
    For VB use the variable as

    Session("myData") = <dataTable> //SET
    <datatbale> = (DataTable)Session("myData") //GET


    Tuesday, January 12, 2010 12:12 AM
  • User-1267218547 posted

    Hi zerberos ,

    From your description, I think you want to share some data with all users.

    If yes, you can use the Cache. since the Cache object is based on all users in the Web Application,

    Cache["d"]=datatable; 

    Then if some user inserts any records, you need to disable the original Cache object and retrieve new data for new Cache object.

    http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

    I hope it is helpful to you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2010 3:45 AM
  • User-952121411 posted

    Now I have a Problem. The table for User 1 and User 2,3.... are the same. That means, when user1 add a row, user 2 see the row too.

    I think this is due to the variable.


     zerberos -

     I re-read your original post several times, and it appears that you indicate that the problem is all user's are seeing the same data and you do not want this.  If this is true, then using Session as described in my prior post will be a solution.

    However, if you do want all users to see the datatable and the problem was it could not be seen by everyone, then use the previously provided suggestions of using the Cache or an Application variable.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2010 11:01 AM