.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 21st, 2009, 07:03 AM
pts69 pts69 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 27 m 52 sec
Reputation Power: 8
Object reference not set to an instance of an object.

I have written a custom namespace for my data access, the code behind and main aspx pages call a function within my namespace to retrieve some data for example:

ASPX Page:
<%=GetStringResource("SEOSiteTitle", Session("User_Locale")) %>

The session var holds "en-GB" for this example


The GetStringResource function looks like this:

Code:
        Public Shared Function GetStringResource(ByVal ResourceName As String, ByVal Locale As String) As String
            If Not String.IsNullOrEmpty(Locale) Then
                Try
                    Dim dt As New DataTable("Table")
                    dt = InlineQuerySQL("Select ConfigValue from Stringresource where Name = '" & ResourceName & "' AND LocaleSetting = '" & Locale & "'")
                    If dt.Rows.Count > 0 Then
                        Return (dt.Rows(0).Item("ConfigValue").ToString)
                    Else
                        Return ""
                    End If
                    dt.Dispose()
                Catch ex As Exception
                    Return (ex.Message.ToString)
                End Try
            Else
                Return "Please supply a locale"
            End If
        End Function
 



This function then calls another function to perform the actual SQL

Code:
Public Shared Function InlineQuerySQL(ByVal strSQL As String) As DataTable
            Try
                Dim SQLConn As New SqlConnection(strConnection)
                Dim sqlCommand As New SqlCommand
                With sqlCommand
                    .CommandText = strSQL
                    .Connection = SQLConn
                    .CommandType = CommandType.Text
                    .Connection.Open()
                    .ExecuteNonQuery()
                    Dim dataAdapter As New SqlDataAdapter(sqlCommand)
                    Dim dt As New DataTable()
                    dataAdapter.Fill(dt)
                    Return dt
                    .Connection.Close()
                    .Dispose()
                    dt.Dispose()
                    dataAdapter.Dispose()
                End With
                SQLConn.Close()
                SQLConn.Dispose()
            Catch ex As Exception
                Return Nothing
                'Response.Write(ex.ToString)
            End Try
        End Function


The problem I have is that I keep getting a intermittent error - " Object reference not set to an instance of an object "

This error occur on line "If dt.Rows.Count > 0 Then" of the GetStringResource function.

Does anyone have any ideas why I get this intermittent error.

Thanks

Paul

Last edited by pts69 : October 21st, 2009 at 08:03 AM.

Reply With Quote
  #2  
Old October 21st, 2009, 09:27 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,446 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 23 h 38 m 21 sec
Reputation Power: 182
If the try in your InlineQuerySQL function fails, your function returns Nothing (which sets the DataTable DT to nothing in the calling function GetStringResource. You can either return an empty datatable instead or test for DT = Nothing in the calling function before testing for number of rows returned.

ie: If Not DT is Nothing Then
'Test for number of rows returned
__________________
Roger (.NET MCP)

Reply With Quote
  #3  
Old October 21st, 2009, 09:51 AM
pts69 pts69 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 27 m 52 sec
Reputation Power: 8
Thanks for that, I've managed to track the issue down to the InlineQuerySQL function sometimes returning a datatable and sometimes not.

Each time it's performing the same query so still not sure why it's erroring.

Paul

Reply With Quote
  #4  
Old October 21st, 2009, 10:00 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,446 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 23 h 38 m 21 sec
Reputation Power: 182
Put your response.write before the return statement and make it Response.Write(ex.Message).

Reply With Quote
  #5  
Old October 21st, 2009, 10:22 AM
pts69 pts69 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 27 m 52 sec
Reputation Power: 8
I think we're getting somewhere now, I couldn't use response.write in namespace, so outputted the exception to a text file.

The actual error I am getting is:

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

Thanks

Paul

Reply With Quote
  #6  
Old October 21st, 2009, 10:24 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,446 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 23 h 38 m 21 sec
Reputation Power: 182
What does your connection string look like?

Reply With Quote
  #7  
Old October 22nd, 2009, 07:09 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,446 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 23 h 38 m 21 sec
Reputation Power: 182
I see you're returning the datatable before closing and disposing of your connection. Try this:

Code:
Public Shared Function InlineQuerySQL(ByVal strSQL As String) As DataTable
            Try
                Dim SQLConn As New SqlConnection(strConnection)
                Dim sqlCommand As New SqlCommand
                With sqlCommand
                    .CommandText = strSQL
                    .Connection = SQLConn
                    .CommandType = CommandType.Text
                    .Connection.Open()
                    .ExecuteNonQuery()
                    Dim dataAdapter As New SqlDataAdapter(sqlCommand)
                    Dim dt As New DataTable()
                    dataAdapter.Fill(dt)
                    .Connection.Close()
                    .Dispose()
                    dt.Dispose()
                    dataAdapter.Dispose()
                End With
                SQLConn.Close()
                SQLConn.Dispose()
                Return dt          'return datatable after cleanup 
            Catch ex As Exception
                Return Nothing
                'Response.Write(ex.ToString)
            End Try
        End Function

Reply With Quote
  #8  
Old October 22nd, 2009, 08:06 AM
pts69 pts69 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level)pts69 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 27 m 52 sec
Reputation Power: 8
Thanks for all the help.

I've managed to get this sorted by doing the following:

Code:
Dim SQLConn
Dim SQLCommand

Try
   Do my Stuff
Catch
   Catch and Log the exception
Finally
   Dispose of SQLConn
   Dispose of SQLCommand
End Try


By adding the finally, I can make sure that all connections are close whether try passes or fails.

Thanks all

Paul

Reply With Quote
Reply

Viewing: ASP Free ForumsProgramming.NET Development > Object reference not set to an instance of an object.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek