Closing SQL connection after return statement

Hi i have a following method
Public Function Bind() As DataSet
     Dim sqlConn As SqlConnection = New SqlConnection(CManager.AppSettings("ConnectionString"))
     Dim adapter As SqlDataAdapter = New SqlDataAdapter("Get", sqlConn)
     Dim ds As New DataSet("ds")

     adapter.SelectCommand.CommandType = CommandType.StoredProcedure
     adapter.Fill(ds)

     Return ds

     adapter.Dispose()
     sqlConn.Close()
End Function

i am closing the sql connection and disposing the dataadapter after teh RETURN statement,will any statement after return execute ...?can any one tell me whats the best approach to go about it.
Thanks
aspnetdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>will any statement after return execute
no.

0
sudheeshthegreatCommented:
No, once a return statement is encountered, all further statements are ignored. I am surprised you didnt find that out during compile time. One of the known methods to dispose/close such resource-intensive objects is to put such statements in the finally block. You may or may not want to use a catch block. Below is a code snippet. And, of course, the return statement needs to be the logically last statement of the method.
try
{
  // use the objects
}
finally
{
  // close ...
  // dispose ...
}
return ...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
burakiewiczCommented:
you could use the using statement to do it for you
using (SqlConnection mycon = new SqlConnection(dbConn))
            {
                using (SqlDataAdapter myad = new SqlDataAdapter("Get",mycon)
                {
                    myad.SelectCommand.CommandType= CommandType.StoredProcedure;
                    myad.Fill(ds);
                }
            }
            return ds;

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

sudheeshthegreatCommented:
burakiewiczDate: that's true for C#. But, if you notice the code snippet that aspnetdev shows is in VB.Net :-)
0
burakiewiczCommented:
didnt check that close, its in the C# section so i figured it was C#, but the using statement is also available in VB
0
sudheeshthegreatCommented:
'using' in vb.net? that's cool. wasnt there when i last checked.
0
burakiewiczCommented:
yeah its there in VS2005
heres a link from msdn. not a great one though
http://msdn2.microsoft.com/en-us/library/htd05whh(VS.80).aspx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.