473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DB connection is not released.

Hi everyone,
I am using this code to access the database.... and after the method is
called... the database connection is not released. The connection is only
released when the application terminates. I used to only close the
connection, now I even called the dispose method of both the SQLCommand and
the SQLConnection objects. what else is missing ???

Private Function CreateCall(ByVal SQL As String) As Double

Dim objCon As SqlConnection

Dim cmd As SqlCommand

Dim ConStr As String

Dim dr As SqlDataReader

ConStr = GetConnectionString()

objCon = New SqlConnection(ConStr)

Try

objCon.Open()

Catch ex As Exception

LogError("CreateCall::Error : " & ex.ToString)

Return -1

End Try

Try

cmd = New SqlCommand(SQL, objCon)

dr = cmd.ExecuteReader

If dr.HasRows Then

dr.Read()

Return dr.GetValue(0)

Else

Return -1

End If

Catch ex As Exception

LogError("CreateCall::Error : " & ex.ToString)

Return -1

Finally

Try

If Not cmd Is Nothing Then

cmd.Dispose()

End If

cmd = Nothing

If Not objCon Is Nothing Then

objCon.Close()

objCon.Dispose()

End If

objCon = Nothing

Catch ex As Exception

End Try

End Try

End Function

Thanks,

Helene
Nov 20 '05 #1
2 1055
Try to close your Datareader
dr.close

"Helene Day" <noSpam@noSpam> a écrit dans le message de
news:uL**************@TK2MSFTNGP09.phx.gbl...
Hi everyone,
I am using this code to access the database.... and after the method is
called... the database connection is not released. The connection is only
released when the application terminates. I used to only close the
connection, now I even called the dispose method of both the SQLCommand and the SQLConnection objects. what else is missing ???

Private Function CreateCall(ByVal SQL As String) As Double

Dim objCon As SqlConnection

Dim cmd As SqlCommand

Dim ConStr As String

Dim dr As SqlDataReader

ConStr = GetConnectionString()

objCon = New SqlConnection(ConStr)

Try

objCon.Open()

Catch ex As Exception

LogError("CreateCall::Error : " & ex.ToString)

Return -1

End Try

Try

cmd = New SqlCommand(SQL, objCon)

dr = cmd.ExecuteReader

If dr.HasRows Then

dr.Read()

Return dr.GetValue(0)

Else

Return -1

End If

Catch ex As Exception

LogError("CreateCall::Error : " & ex.ToString)

Return -1

Finally

Try

If Not cmd Is Nothing Then

cmd.Dispose()

End If

cmd = Nothing

If Not objCon Is Nothing Then

objCon.Close()

objCon.Dispose()

End If

objCon = Nothing

Catch ex As Exception

End Try

End Try

End Function

Thanks,

Helene

Nov 20 '05 #2
Don't use a DataReader to return a single value.

Dim o As Object
o = cmd.ExecuteScalar
If o Is Nothing Then
Return = -1
Else
Return = CInt(o)
End If

Maybe somebody else can come up with a more elegant way of testing for an
empty result set.

HTH,
Greg
"Helene Day" <noSpam@noSpam> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
Hi everyone,
I am using this code to access the database.... and after the method is
called... the database connection is not released. The connection is only
released when the application terminates. I used to only close the
connection, now I even called the dispose method of both the SQLCommand and the SQLConnection objects. what else is missing ???

Private Function CreateCall(ByVal SQL As String) As Double

Dim objCon As SqlConnection

Dim cmd As SqlCommand

Dim ConStr As String

Dim dr As SqlDataReader

ConStr = GetConnectionString()

objCon = New SqlConnection(ConStr)

Try

objCon.Open()

Catch ex As Exception

LogError("CreateCall::Error : " & ex.ToString)

Return -1

End Try

Try

cmd = New SqlCommand(SQL, objCon)

dr = cmd.ExecuteReader

If dr.HasRows Then

dr.Read()

Return dr.GetValue(0)

Else

Return -1

End If

Catch ex As Exception

LogError("CreateCall::Error : " & ex.ToString)

Return -1

Finally

Try

If Not cmd Is Nothing Then

cmd.Dispose()

End If

cmd = Nothing

If Not objCon Is Nothing Then

objCon.Close()

objCon.Dispose()

End If

objCon = Nothing

Catch ex As Exception

End Try

End Try

End Function

Thanks,

Helene

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: DP | last post by:
I read some articles and post how to optimize te speed of asp pages regarding opening and closing DB connections but I am still not sure about this. Is it true that it doesn't matter how many...
5
by: DaM | last post by:
Hi guys, I'm having this problem with my ASP.Net application: I was testing the whole site, and it seem to work fine. Fast and stable, but suddenly it stopped working and this error occurred:...
16
by: Jozef | last post by:
Hello, I'm having the following issue; I'm having trouble referencing these public variables after they are being set, and I'm not sure why. I use the code below to set open a connection to...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
8
by: Ike | last post by:
Is anyone aware of a means of connection pooling (to MySQL, say) in php? Thanks, Ike
3
by: howachen | last post by:
Hi, Is that when using pconnect to mysql, it is already mean connection pooling? thanks.
2
by: JoeSep | last post by:
Hi! Is it correct/safe to define a connection pool in the string "sqlConnectionString" of the "sessionState" section of Web.config? - The application is developed using AspNet 1.1 in a Windows...
7
by: fniles | last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection). I read using DataAdapter and DataSet, not DataReader. When many people try to access the database at the same time, I get...
0
by: nospam | last post by:
Hello everyone, I have a newbie question that I am sure you have probably encountered. I am developing a ASP NET app that interfaces to a database ( SqlServer). When I am developing I use my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.