where should I dispose the connection ?

Y

ypul

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.Get("ConnectionString"))

con.Open()

Return con

Catch ex As SqlException

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Function
 
A

Alexander Shirshov

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
 
G

Guest

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.
 
Y

ypul

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
 
A

Alexander Shirshov

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



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
 
Y

ypul

Thanks alex ...u were a good help ...
cheers
vips
Alexander Shirshov said:
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



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top