469,898 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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 1639
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Henrik Dahl | last post: by
15 posts views Thread by Sam Sungshik Kong | last post: by
17 posts views Thread by Bob Lehmann | last post: by
8 posts views Thread by Pierson C | last post: by
156 posts views Thread by Dennis | last post: by
18 posts views Thread by Eric B. | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.