473,323 Members | 1,589 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,323 software developers and data experts.

where should I dispose the connection ?

the code below given is connection class ... now I want to use the
connection in another class , by using the getConnection method.

where should I call con.dispose() ?

in connection class or in the caller class ?

if I call con.dispose in connection class as given below ..will I be able to
use the connection in the other class where I am calling this method ..or I
will get a NULL connection ??

-------------------------Code -------------------------

Public Class Connection

Public Shared Function getConnection() As SqlConnection

Dim con As SqlConnection

Try

con = New
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))

con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function
Nov 19 '05 #1
5 1770
Because of Finally block your method always return closed and disposed
connection, not very useful... Remember, Finally blocks ALWAYS execute.

You must return opened connection and let callers dispose it when they have
finished working with it.

HTH,
Alexander Shirshov

"ypul" <yp**@hotmail.com> wrote in message
news:ez**************@TK2MSFTNGP14.phx.gbl...
the code below given is connection class ... now I want to use the
connection in another class , by using the getConnection method.

where should I call con.dispose() ?

in connection class or in the caller class ?

if I call con.dispose in connection class as given below ..will I be able
to
use the connection in the other class where I am calling this method ..or
I
will get a NULL connection ??

-------------------------Code -------------------------

Public Class Connection

Public Shared Function getConnection() As SqlConnection

Dim con As SqlConnection

Try

con = New
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))

con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function

Nov 19 '05 #2
Hi
You may use it wherever you like, just be sure to close it after the work is
done or if an exception occured, because it may cause some problems.
"ypul" wrote:
the code below given is connection class ... now I want to use the
connection in another class , by using the getConnection method.

where should I call con.dispose() ?

in connection class or in the caller class ?

if I call con.dispose in connection class as given below ..will I be able to
use the connection in the other class where I am calling this method ..or I
will get a NULL connection ??

-------------------------Code -------------------------

Public Class Connection

Public Shared Function getConnection() As SqlConnection

Dim con As SqlConnection

Try

con = New
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))

con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function

Nov 19 '05 #3
thanks alex
I have a doubt ..
when I return a connection to the caller class will it create a copy of the connection object or it will pass the same object ??

In the code below ...is the "Con" pointing to the same object of getconnection...??..

if yes then i should dispose "con" in caller1 class ... and there is no danger of undisposed object which I created in the "Connection" class, RIGHT ?

Public Class caller1

Public Function getAll() As DataTable

Dim dt As DataTable

Dim da As SqlDataAdapter

Dim dc As SqlCommand

Dim strsql As String

Dim con As SqlConnection

Try

' get the data from the dataLayer

dt = New DataTable()

con = Connection.getConnection

dc = New SqlCommand(strsql, con)

da = New SqlDataAdapter(dc)

da.Fill(dt)

Return dt

Catch ex As Exception

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function

End Class

"Alexander Shirshov" <al*******@omnitalented.com> wrote in message news:ut**************@TK2MSFTNGP14.phx.gbl...
Because of Finally block your method always return closed and disposed
connection, not very useful... Remember, Finally blocks ALWAYS execute.

You must return opened connection and let callers dispose it when they have
finished working with it.

HTH,
Alexander Shirshov



"ypul" <yp**@hotmail.com> wrote in message
news:ez**************@TK2MSFTNGP14.phx.gbl...
the code below given is connection class ... now I want to use the
connection in another class , by using the getConnection method.

where should I call con.dispose() ?

in connection class or in the caller class ?

if I call con.dispose in connection class as given below ..will I be able
to
use the connection in the other class where I am calling this method ...or
I
will get a NULL connection ??

-------------------------Code -------------------------

Public Class Connection

Public Shared Function getConnection() As SqlConnection

Dim con As SqlConnection

Try

con = New
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))

con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function



Nov 19 '05 #4
Con variable points to the same connection object you created in
GetConnection, sure.

Right now you're doing everything correctly... almost. You should check that
connection was opened before closing it otherwise you'll get the exception
in your Finally block:

If conn.State = ConnectionState.Open
conn.Close()
End If

"ypul" <yp**@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...
thanks alex
I have a doubt ..
when I return a connection to the caller class will it create a copy of the
connection object or it will pass the same object ??

In the code below ...is the "Con" pointing to the same object of
getconnection...??..
if yes then i should dispose "con" in caller1 class ... and there is no
danger of undisposed object which I created in the "Connection" class, RIGHT
?
Public Class caller1
Public Function getAll() As DataTable
Dim dt As DataTable
Dim da As SqlDataAdapter
Dim dc As SqlCommand
Dim strsql As String
Dim con As SqlConnection
Try
' get the data from the dataLayer
dt = New DataTable()
con = Connection.getConnection

dc = New SqlCommand(strsql, con)
da = New SqlDataAdapter(dc)
da.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Function

End Class
"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
Because of Finally block your method always return closed and disposed
connection, not very useful... Remember, Finally blocks ALWAYS execute.

You must return opened connection and let callers dispose it when they
have
finished working with it.

HTH,
Alexander Shirshov

"ypul" <yp**@hotmail.com> wrote in message
news:ez**************@TK2MSFTNGP14.phx.gbl...
the code below given is connection class ... now I want to use the
connection in another class , by using the getConnection method.

where should I call con.dispose() ?

in connection class or in the caller class ?

if I call con.dispose in connection class as given below ..will I be
able
to
use the connection in the other class where I am calling this method
..or
I
will get a NULL connection ??

-------------------------Code -------------------------

Public Class Connection

Public Shared Function getConnection() As SqlConnection

Dim con As SqlConnection

Try

con = New
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))

con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function



Nov 19 '05 #5
Thanks alex ...u were a good help ...
cheers
vips
"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:uW**************@TK2MSFTNGP12.phx.gbl...
Con variable points to the same connection object you created in
GetConnection, sure.

Right now you're doing everything correctly... almost. You should check that connection was opened before closing it otherwise you'll get the exception
in your Finally block:

If conn.State = ConnectionState.Open
conn.Close()
End If

"ypul" <yp**@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...
thanks alex
I have a doubt ..
when I return a connection to the caller class will it create a copy of the connection object or it will pass the same object ??

In the code below ...is the "Con" pointing to the same object of
getconnection...??..
if yes then i should dispose "con" in caller1 class ... and there is no
danger of undisposed object which I created in the "Connection" class, RIGHT ?
Public Class caller1
Public Function getAll() As DataTable
Dim dt As DataTable
Dim da As SqlDataAdapter
Dim dc As SqlCommand
Dim strsql As String
Dim con As SqlConnection
Try
' get the data from the dataLayer
dt = New DataTable()
con = Connection.getConnection

dc = New SqlCommand(strsql, con)
da = New SqlDataAdapter(dc)
da.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Function

End Class
"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
Because of Finally block your method always return closed and disposed
connection, not very useful... Remember, Finally blocks ALWAYS execute.

You must return opened connection and let callers dispose it when they
have
finished working with it.

HTH,
Alexander Shirshov

"ypul" <yp**@hotmail.com> wrote in message
news:ez**************@TK2MSFTNGP14.phx.gbl...
the code below given is connection class ... now I want to use the
connection in another class , by using the getConnection method.

where should I call con.dispose() ?

in connection class or in the caller class ?

if I call con.dispose in connection class as given below ..will I be
able
to
use the connection in the other class where I am calling this method
..or
I
will get a NULL connection ??

-------------------------Code -------------------------

Public Class Connection

Public Shared Function getConnection() As SqlConnection

Dim con As SqlConnection

Try

con = New
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))
con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function


Nov 19 '05 #6

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

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
17
by: Bob Lehmann | last post by:
Hi, My understanding is that Dispose() should not be used for destroying a connection object, and that Close() is preferred. However, in one of MS's Quickstart Apps I see this being used.... ...
8
by: Pierson C | last post by:
I am developing on a website that is utilizing SQL Server 2000. Shortly after deploying the site, we began having timeout issues due to the max connections. 1st instinct was to diligently tidy...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
18
by: Eric B. | last post by:
Is it a bad idea to do both once I'm done with an object? Eric B.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.