using "return" inside "using" block in a web service

C

cj2

If I have a web service like:

<WebMethod()> _
Public Function Validate(ByVal order As String, ByVal requestor As
String) As String

Using mySqlConnection As New SqlConnection(...connection string...)
do some stuff
if something
return "Orders shipped"
endif
do lots and lots more stuff
end using
return orderstatus
end function

If the program determines early in the code that the order is shipped
and I just return from there that it's shipped--I've jumped out of the
using block--does mySqlConnection get closed up and disposed of just
like I'd exited the using block or do I have to pass the end using in
order for the sql connection to get cleaned up?
 
R

rowe_newsgroups

If I have a web service like:

<WebMethod()> _
Public Function Validate(ByVal order As String, ByVal requestor As
String) As String

    Using mySqlConnection As New SqlConnection(...connection string....)
       do some stuff
       if something
          return "Orders shipped"
       endif
       do lots and lots more stuff
    end using
return orderstatus
end function

If the program determines early in the code that the order is shipped
and I just return from there that it's shipped--I've jumped out of the
using block--does mySqlConnection get closed up and disposed of just
like I'd exited the using block or do I have to pass the end using in
order for the sql connection to get cleaned up?

It gets closed.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
M

Michel Posseth [MCP]

The answer in short is yes you are right with your guess

the using block does the same as

<WebMethod()> _
Public Function Validate(ByVal order As String, ByVal requestor As String)
As String
Dim mySqlConnection As New SqlConnection(...connection string...)
Try
do some stuff
if something
return "Orders shipped"
endif
do lots and lots more stuff
catch
Finally
mySqlConnection.Close
mySqlConnection.Dispose
End try
return orderstatus
end function

HTH

Michel
 
C

cj2

Thanks. Was using available in VB 2005? I don't recall. Anyway I take
it it is the new preferred way to do things like database connections.

I'd still want to use try catch around mySqlConnection.open and reading
records as it would allow me to take some kind of action if there was an
error. But using should ensure the connection is closed no matter what
happens to cause the function to end. I can't risk leaving connections
open.
 
M

Michel Posseth [MCP]

cj2
The using stament is a framework 2.0 feature so yes it was availlable in
VB.Net 2005
Anyway I take it it is the new preferred way to do things like database
connections.
It is the prefered way for all objects that implement the idisposable
interface , if in doubt declare with using and VB wil correct you inmediatly
with a message that the object does not implement idisposable
But using should ensure the connection is closed no matter what happens to
cause the function to end. I can't risk leaving connections open.

Yes and it gives you a method level scope , wich can be verry handy


hth

Michel Posseth
 
S

Steven Cheng

Hi Cj,

Since the using block wrapper all the inner code, as long as the return is
within the wrappered code block, the resource will be correctly disposed.

Just like you use a try ....catch ...finally block to ensure the finalize
code execution.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we

can improve the support we provide to you. Please feel free to let my
manager know what you think of

the level of service provided. You can send feedback directly to my manager
at: (e-mail address removed).

--------------------
 

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

Top