Results 1 to 6 of 6

Thread: Difficult problem error -sesssion variable workaround

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113

    Difficult problem error -sesssion variable workaround

    Hi all,
    I am new to .Net and have a frustrating problem.

    We have a peice of code that works, but every now and then we get the attached error. It works fine, but then after many transactions this error pops up. Once the error has popped up - we cant use the page for a while (maybe until the session times out.)

    Please take alook...thanks
    error:
    VB Code:
    1. Object reference not set to an instance of an object.
    2. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    3.  
    4. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    5.  
    6. Source Error:
    7.  
    8.  
    9. Line 37:         'Response.Write(drData)
    10. Line 38:         'Response.End()
    11. Line 39:    While drData.Read
    12. Line 40:             strAspxPageToCall = drData("AspxPageToCall")
    13. Line 41:             '   Response.Write(strAspxPageToCall)
    14.  
    15.  
    16. Source File: \\Prxdevtest\Projects\LGWSETA\AdminTools\ToAspxDisplay.aspx.vb    Line: 39
    17.  
    18. Stack Trace:
    19.  
    20.  
    21. [NullReferenceException: Object reference not set to an instance of an object.]
    22.    AdminTools.ToAspxDisplay.Page_Load(Object sender, EventArgs e) in \\Prxdevtest\Projects\LGWSETA\AdminTools\ToAspxDisplay.aspx.vb:39
    23.    System.Web.UI.Control.OnLoad(EventArgs e) +67
    24.    System.Web.UI.Control.LoadRecursive() +29
    25.    System.Web.UI.Page.ProcessRequestMain() +724

    The code is a work around because we cant pass session variables in .net - so we write the session variables to a database and read them back.
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113

    the code

    here is the code for the page the error refers to...

    VB Code:
    1. Public Class ToAspxDisplay
    2.     Inherits System.Web.UI.Page
    3.  
    4. #Region " Web Form Designer Generated Code "
    5.  
    6.     'This call is required by the Web Form Designer.
    7.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    8.  
    9.     End Sub
    10.  
    11.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    12.         'CODEGEN: This method call is required by the Web Form Designer
    13.         'Do not modify it using the code editor.
    14.         InitializeComponent()
    15.     End Sub
    16.  
    17. #End Region
    18.  
    19.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.     Dim intXaToAspxNo As Integer
    21.     Dim strSql, strAspxPageToCall As String
    22.     Dim clsPraxisDb As New AdminTools.prx.db_lib()
    23.     Dim drData As SqlClient.SqlDataReader
    24.    
    25.     intXaToAspxNo = Request.QueryString("Value")
    26.  
    27.     strSql = "SELECT SessionName, SessionValue, AspxPageToCall " & _
    28.           "FROM xaToAspx INNER JOIN xaToAspxSessionList ON xaToAspx.xaToAspxNo = xaToAspxSessionList.xaToAspxNo " & _
    29.           "INNER JOIN xaPageAndSession ON xaToAspx.xaPageAndSessionNo = xaPageAndSession.xaPageAndSessionNo " & _
    30.           "WHERE xaToAspx.xaToAspxNo = " & intXaToAspxNo
    31.     clsPraxisDb.DBConnectionString = Application.Contents("ConnectionString")
    32.     clsPraxisDb.makeSQLConnection()
    33.     clsPraxisDb.SQLStatement = strSql
    34.     drData = clsPraxisDb.getRecords
    35.  
    36.    While drData.Read
    37.       strAspxPageToCall = drData("AspxPageToCall")
    38.       Session.Add(drData("SessionName"), drData("SessionValue"))
    39.     End While
    40.     clsPraxisDb.execNonQuery("DELETE FROM xaToAspxSessionList WHERE XaToAspxNo = " & intXaToAspxNo)
    41.     clsPraxisDb.execNonQuery("DELETE FROM xaToAspx WHERE XaToAspxNo = " & intXaToAspxNo)
    42.  
    43.     Response.Redirect(strAspxPageToCall)
    44.   End Sub
    45.  
    46. End Class
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113
    I must be honest,
    I have never used .Net before. The guy that did the above code left and I have no clue how to fix.

    Shoudl he have closed the datasets and things like that??

    i.e. set clsPraxisdb = nothing or .close....

    or something along thos lines?

    I know that in ASP or VB I also set them to nothing or close them to make sure they dont hang around....

    or clsPraxisDB.closeSQLConnection()


    Thanks in advance everyone who helps...
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well, I'm not sure what the insides of that class do, but you'll wanna test and see if your DataReader returned from the getRecords method is null(nothing):
    VB Code:
    1. If Not drData Is Nothing Then
    2.   While drData.Read
    3.     ...
    4.   End While
    5. End If
    This is pretty horrible code, you guys should be glad the original author is no longer with you. You might wanna check this out for reference http://msdn.microsoft.com/library/de...Guidelines.asp

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113
    Thanks, I will try that and I agree that the code is terrible. ALso, thanks for the link - I have sent it around and will make it part of our .NET coding standards.

    The thing is that its unlikely that the data is null. The error seems to occur either after a certain number of transactions OR when two people log in and use the page at the same time.

    Any one ever heard of anything like that before? And dont you think it has anything to do with setting the recordsets to nothing?
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Generally you'll want to use a Close and Dispose pattern for database stuff; and you'll wrap that code inside of Try..Catch blocks. Setting objects to nothing or null is different in .net; it's not like in vb6 and earlier where the reference count to your object is decremented by 1 by setting your object variable to nothing. It's all garbage collection these days. You'll have to check out that other guys(or girls) class that handles the database code to ensure that it ALWAYS closes an open connection and always disposes of any object that supports IDisposable(like the connection and command objects), here are some examples:
    - using the close and dispose pattern:
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.SqlClient
    4.  
    5. Public Class CloseDisposePattern : Inherits System.Web.UI.Page
    6.     Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    7.         Dim connString As String = "user id=sa;password=sa;database=scratch;server=(local);"
    8.         Dim cn As New SqlConnection(connString)
    9.         Dim cmdText As String = "Select * From..."
    10.         Dim cmd As New SqlCommand(cmdText, cn)
    11.         Dim dr As SqlDataReader
    12.         Try
    13.             cmd.Connection.Open()
    14.             dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    15.             If Not dr Is Nothing Then
    16.                 While dr.Read
    17.                     'do something with the datareader
    18.                 End While
    19.             End If
    20.         Finally
    21.             If Not dr Is Nothing Then
    22.                 dr.Close()
    23.             End If
    24.             If Not cn Is Nothing Then
    25.                 cn.Close()
    26.                 cn.Dispose()
    27.             End If
    28.             If Not cmd Is Nothing Then
    29.                 cmd.Dispose()
    30.             End If
    31.         End Try
    32.     End Sub
    33. End Class
    This can get to be a lot of typing so it's common to write a helper class that handles the grunt work:
    VB Code:
    1. Public Class DAL : Implements IDisposable
    2.  
    3.     Private _disposed As Boolean = False
    4.     Private _cn As SqlConnection
    5.     Private _connString As String
    6.  
    7.     Public Function ExecuteReader(ByVal cmd As SqlCommand) As SqlDataReader
    8.         initConnection()
    9.         cmd.Connection = _cn
    10.         Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
    11.     End Function
    12.  
    13.     Public Sub New()
    14.         _connString = "user id=sa;password=sa;database=scratch;server=(local);"
    15.     End Sub
    16.     Private Sub initConnection()
    17.         _cn = New SqlConnection(_connString)
    18.     End Sub
    19.     Sub Dispose() Implements IDisposable.Dispose
    20.         If _disposed = False Then
    21.             cleanUp()
    22.             _disposed = True
    23.             'Make sure the Finalize method is not called
    24.             GC.SuppressFinalize(Me)
    25.         End If
    26.     End Sub
    27.     Protected Overrides Sub Finalize()
    28.         cleanUp()
    29.     End Sub
    30.     Private Sub cleanUp()
    31.         If Not _cn Is Nothing Then
    32.             _cn.Close()
    33.             _cn.Dispose()
    34.         End If
    35.     End Sub
    36. End Class
    Then all the client would do to use this would be:
    VB Code:
    1. Public Class CloseDisposePattern : Inherits System.Web.UI.Page
    2.     Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    3.         Dim cmdText As String = "Select * From..."
    4.         Dim cmd As New SqlCommand(cmdText)
    5.         Dim dr As SqlDataReader
    6.         Dim dal As New DAL
    7.         Try
    8.             dr = dal.ExecuteReader(cmd)
    9.             If Not dr Is Nothing Then
    10.                 While dr.Read
    11.                     'do something with the datareader
    12.                 End While
    13.             End If
    14.         Finally
    15.             If Not dr Is Nothing Then
    16.                 dr.Close()
    17.             End If
    18.             If Not cmd Is Nothing Then
    19.                 cmd.Dispose()
    20.             End If
    21.             If Not dal Is Nothing Then
    22.                 dal.Dispose()
    23.             End If
    24.         End Try
    25.     End Sub
    26. End Class

    Now that I'm looking at it, VB.NET sure seems wordy. In C# the same client code looks like this(dispose code for each item in the using statement is automatically handled):
    PHP Code:
    protected override void OnInit(System.EventArgs e)
    {
        
    SqlDataReader dr null;
        
    string cmdText "Select * From ...";
        
    using (DAL dal = new DAL())
        
    using (SqlCommand cmd = new SqlCommand(cmdText))
        {
            try
            {
                
    dr dal.ExecuteReader(cmd);
                if (
    dr != null)
                {
                    while(
    dr.Read())
                    {
                        
    //do something
                    
    }
                }
            }
            finally
            {
                if (
    dr != null)
                {
                    
    dr.Close();
                }
            }
        }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width