none
global (Public Shared) variables RRS feed

  • Question

  • I would like to know why Visual Studio 2005 has issues with global ( Public Shared) variables. I used global variables in a report to pass data to the page header and an instance of the report showed the incorrect patient demographic information in the variables I referenced in the header. In this particular instance the page header showed one patient’s demographic information with the selected patient’s medications.

     

    We are using Visual Studio 2005:

     

    Microsoft Visual Studio 2005

    Version 8.0.50727.762  (SP.050727-7600)

    Microsoft .NET Framework

    Version 2.0.50727 SP2

     

    Installed Edition: Professional

     

    Microsoft Web Application Projects 2005   77626-009-0000007-41181

    Microsoft Web Application Projects 2005

    Version 8.0.50727.762

     

    Crystal Reports    AAC60-G0CSA4B-V7000AY

    Crystal Reports for Visual Studio 2005

     

     

    Microsoft Visual Studio 2005 Professional Edition - ENU Service Pack 1 (KB926601)  

    This service pack is for Microsoft Visual Studio 2005 Professional Edition - ENU.

    If you later install a more recent service pack, this service pack will be uninstalled automatically.

    For more information, visit http://support.microsoft.com/kb/926601

     

    SQL Server Analysis Services  

    Microsoft SQL Server Analysis Services Designer

    Version 9.00.1399.00

     

    SQL Server Integration Services  

    Microsoft SQL Server Integration Services Designer

    Version 9.00.1399.00

     

    SQL Server Reporting Services  

    Microsoft SQL Server Reporting Services Designers

    Version 9.00.1399.00


    My .NET Blog: http://michaelcrump.net
    Monday, July 5, 2010 6:48 PM

Answers

  • Michael,

    Local report refers to those reports not depending on SQL Server Reporting Services but published together with your application. The local report files have the extension .rdlc while the server report files have the extention .rdl. For more information about local report, you can refer to http://msdn.microsoft.com/en-us/library/ms252067(VS.80).aspx.

    From your description, it looks that you were using a SSRS server report, right? In this case, a single "public shared" variable as aforementioned may cause concurrent access issue as you encountered. To use the "public shared" and avoid conflicts caused by concrrent access, an usual solution is to use a hashtable so that you can associate a variable value with a key which identify a client. For example:

    Public Function AddToCount(key As String, val As Integer) As String
    	If ht.ContainsKey(key) Then
    		ht(key) = CInt(ht(key)) +val
    	Else
    		ht.Add(key, val)
    	End If
    	Return ht(key).ToString()
    
    End Function
    
    Public Shared ht As System.Collections.Hashtable = New System.Collections.Hashtable()
    
    Then in your report, you can call this function like "=Code.AddToCount(User!UserID, 1)". In your case, you may use a patient's ID as the key if the shared data is per patient.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, July 7, 2010 8:10 AM
    Moderator

All replies

  • Michael,

    Was your issue for a crystal report or a SSRS local report (.rdlc)? If it is a crystal report, I recommend that you contact BO for further assistance on this issue, http://msdn.microsoft.com/en-us/library/ms225450(v=VS.80).aspx.

    For a SSRS local report, "public shared" should work fine. I performed a test with the following code in a SSRS local report in VS 2005:

    Public Shared Function AddToCount(ByVal Value As Integer) As String
      Count = Count + value
      Return CStr(Count)
    End Function
    
    Public Shared Count As Integer = 0
    

    Then I set three fields values to "=Code.AddToCount(1)". When I browse the report in ReportViewer control, it correctly shows 1, 2, 3 as I expected.

    If you used a SSRS local report, you can refer to my steps to see if there is anything omitted in your steps. If this issue persists, I recommend that you post your code snippets here and let us know how your steps of reproducing this issue, then we can help you dig out the root cause.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Tuesday, July 6, 2010 7:51 AM
    Moderator
  • What does local mean?

     

     

    I am running the report from within an application. The application runs through Citrix servers and the report runs using context sensitive data (selected document for selected patient).

     

    The report worked fine until an incident (Report Services fail) caused the public shared variable to display another patient’s demographic data. I used fields in the page header to display the patient demographic data and in 2 instances over the past several weeks, this mix of 2 patient data occurred.


    My .NET Blog: http://michaelcrump.net
    Tuesday, July 6, 2010 8:02 PM
  • Michael,

    Local report refers to those reports not depending on SQL Server Reporting Services but published together with your application. The local report files have the extension .rdlc while the server report files have the extention .rdl. For more information about local report, you can refer to http://msdn.microsoft.com/en-us/library/ms252067(VS.80).aspx.

    From your description, it looks that you were using a SSRS server report, right? In this case, a single "public shared" variable as aforementioned may cause concurrent access issue as you encountered. To use the "public shared" and avoid conflicts caused by concrrent access, an usual solution is to use a hashtable so that you can associate a variable value with a key which identify a client. For example:

    Public Function AddToCount(key As String, val As Integer) As String
    	If ht.ContainsKey(key) Then
    		ht(key) = CInt(ht(key)) +val
    	Else
    		ht.Add(key, val)
    	End If
    	Return ht(key).ToString()
    
    End Function
    
    Public Shared ht As System.Collections.Hashtable = New System.Collections.Hashtable()
    
    Then in your report, you can call this function like "=Code.AddToCount(User!UserID, 1)". In your case, you may use a patient's ID as the key if the shared data is per patient.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, July 7, 2010 8:10 AM
    Moderator