|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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) |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
Put your response.write before the return statement and make it Response.Write(ex.Message).
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
||||
|
||||
|
What does your connection string look like?
|
|
#7
|
||||
|
||||
|
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
|
|
#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 |
![]() |
| Viewing: ASP Free Forums > Programming > .NET Development > Object reference not set to an instance of an object. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|