473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programming - Best Practice

Hello.

I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...

A number of examples I have followed have the following in their
finally statement

Try
......
Catch
......
Finally

If (Not IsNothing(dbCon n)) Then
dbConn.Close()
End If

End Try

Will my data connection always close using this method or should I
just use dbConn.close?

I have a program that seems to be holding onto its connection - am I
missing something?

Feb 16 '07 #1
51 3659

"bigHairy" <mt*********@gm ail.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello.

I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...

A number of examples I have followed have the following in their
finally statement

Try
.....
Catch
.....
Finally

If (Not IsNothing(dbCon n)) Then
dbConn.Close()
End If

End Try

Will my data connection always close using this method or should I
just use dbConn.close?

I have a program that seems to be holding onto its connection - am I
missing something?
If you just use dbConn.Close(), you'll get an exception if the connection in
question is not open.
The code in finally first checks to see if dbConn is an empty reference (no
active connection), and only attempts to close it is there is an active
connection to close.
As code in the finally block is "guaranteed to execute", the code you posted
closes the connection *if* it is open.
The question is, what makes you think the connection remains active after
this code executes?
Feb 16 '07 #2
Thanks for your prompt reply, I appreciate the help.

I am currently in discussions with a 3rd party data provider as there
seem to be timeout issues using a linked SQL Server with a Progress
Database. There are suggestions that it may be our program leaving
connections open but I can find no evidence of this - thought I would
check that I am using the best practice for closing these connections
as a first port of call before I go investigating the rest of the
process.
Feb 16 '07 #3
You could use something like this to close your connection:

If objConn.State = Data.Connection State.Open Then
objConn.Close()
End If


"bigHairy" <mt*********@gm ail.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello.

I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...

A number of examples I have followed have the following in their
finally statement

Try
.....
Catch
.....
Finally

If (Not IsNothing(dbCon n)) Then
dbConn.Close()
End If

End Try

Will my data connection always close using this method or should I
just use dbConn.close?

I have a program that seems to be holding onto its connection - am I
missing something?

Feb 16 '07 #4
On Feb 16, 11:07 am, "Zim Babwe"
<zimba...@doyou reallythinkthis isreal.comwrote :
You could use something like this to close your connection:

If objConn.State = Data.Connection State.Open Then
objConn.Close()
End If

"bigHairy" <mthomas1...@gm ail.comwrote in message

news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
You could call Dispose instead of Close. Dispose will not throw
exceptions. If you're using VB 2005 the Using keyword can clean up
the code quite a bit as well.

Feb 16 '07 #5
Hi,

AS you ask for Best Practice than you don't definitly not need a test to see
if your connection is open.

You control your program, so you know if it is open or not and can close it
therefore at the right time without any error.

Cor

"Zim Babwe" <zi******@doyou reallythinkthis isreal.comschre ef in bericht
news:Oa******** *****@TK2MSFTNG P04.phx.gbl...
You could use something like this to close your connection:

If objConn.State = Data.Connection State.Open Then
objConn.Close()
End If


"bigHairy" <mt*********@gm ail.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
>Hello.

I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...

A number of examples I have followed have the following in their
finally statement

Try
.....
Catch
.....
Finally

If (Not IsNothing(dbCon n)) Then
dbConn.Close ()
End If

End Try

Will my data connection always close using this method or should I
just use dbConn.close?

I have a program that seems to be holding onto its connection - am I
missing something?


Feb 16 '07 #6
The issue here has nothing to do with whether or not the database connection
is open. It is to do with whether or not the database connection object has
been instantiated.

You will probabaly note that the 'examples' have the dbConn variable
declared before the Try. This means that the dbConn variable is 'scoped' so
that it is available to the Finally block.

With both the SqlConnection and OleDbConnection classes, calling Close on an
object of those types that is not open will NOT cause an exception. However,
if you call Close on an object of those types that has NOT been instantiated
(Is Nothing) then an exception WILL be thrown.

In the Try block, if an exception is thrown before dbConn is instantiated
(let alone opened), the logic will 'jump' directly to the Catch block and
then fall through to the Finally block. At this point dbConn would be
Nothing and a call to dbConn.Close would fail. The conditional execution of
dbConn.Close handles this situation.

Because the code Finally block is executed regardless of whether an
exception was thrown or not, having the conditional execution of
dbConn.Close means that you do not need to worrying about closing the
connection in either the Try or Catch blocks.
"bigHairy" <mt*********@gm ail.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello.

I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...

A number of examples I have followed have the following in their
finally statement

Try
.....
Catch
.....
Finally

If (Not IsNothing(dbCon n)) Then
dbConn.Close()
End If

End Try

Will my data connection always close using this method or should I
just use dbConn.close?

I have a program that seems to be holding onto its connection - am I
missing something?
Feb 16 '07 #7
I didn't think that you could even leave a connection open.

it's one of my biggest complaints about ADO.net; I used to use @@SPID
similiar to sessionID in ASP in order to build some simple apps...

but now there is nothing like that in .NET from what I understand

I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?
like I'm being serious and honest here.

Thanks

On Feb 16, 12:04 pm, "Stephany Young" <noone@localhos twrote:
The issue here has nothing to do with whether or not the database connection
is open. It is to do with whether or not the database connection object has
been instantiated.

You will probabaly note that the 'examples' have the dbConn variable
declared before the Try. This means that the dbConn variable is 'scoped' so
that it is available to the Finally block.

With both the SqlConnection and OleDbConnection classes, calling Close on an
object of those types that is not open will NOT cause an exception. However,
if you call Close on an object of those types that has NOT been instantiated
(Is Nothing) then an exception WILL be thrown.

In the Try block, if an exception is thrown before dbConn is instantiated
(let alone opened), the logic will 'jump' directly to the Catch block and
then fall through to the Finally block. At this point dbConn would be
Nothing and a call to dbConn.Close would fail. The conditional execution of
dbConn.Close handles this situation.

Because the code Finally block is executed regardless of whether an
exception was thrown or not, having the conditional execution of
dbConn.Close means that you do not need to worrying about closing the
connection in either the Try or Catch blocks.

"bigHairy" <mthomas1...@gm ail.comwrote in message

news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello.
I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...
A number of examples I have followed have the following in their
finally statement
Try
.....
Catch
.....
Finally
If (Not IsNothing(dbCon n)) Then
dbConn.Close()
End If
End Try
Will my data connection always close using this method or should I
just use dbConn.close?
I have a program that seems to be holding onto its connection - am I
missing something?

Feb 16 '07 #8
On Feb 16, 3:00 pm, pfc_s...@hotmai l.com wrote:
I didn't think that you could even leave a connection open.

it's one of my biggest complaints about ADO.net; I used to use @@SPID
similiar to sessionID in ASP in order to build some simple apps...

but now there is nothing like that in .NET from what I understand

I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?

like I'm being serious and honest here.

Thanks
Hi,

What made you think you couldn't leave a connection open?

Brian

Feb 16 '07 #9
uh a half dozen MS press books???
uh a half dozen MS press books???
uh a half dozen MS press books???
get a spid; get another SPID.

DO THEY MATCH?

because if they do then I'll accept a written apology from Microsoft
and new editions of their books that are CORRECTED.
On Feb 16, 1:48 pm, "Brian Gideon" <briangid...@ya hoo.comwrote:
On Feb 16, 3:00 pm, pfc_s...@hotmai l.com wrote:
I didn't think that you could even leave a connection open.
it's one of my biggest complaints about ADO.net; I used to use @@SPID
similiar to sessionID in ASP in order to build some simple apps...
but now there is nothing like that in .NET from what I understand
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?
like I'm being serious and honest here.
Thanks

Hi,

What made you think you couldn't leave a connection open?

Brian

Feb 16 '07 #10

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

Similar topics

136
9439
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
12
8479
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now I really want to get back into it as I see a point with GNU/Linux. I want to get my old skills back and write something or help on some projects etc. I need some good books. I used to have one called "A Book On C", but sold it,
8
2386
by: Dale | last post by:
I've searched Amazon and read probably 100 reviews but can't find what seems to be any book that is widely accepted as the definitive book on object oriented programming design and techniques. And most of the highest rated are all written 10 to 15 years ago. Any good suggestions?
19
2299
by: JoeC | last post by:
I have seen many books that teack coding for C++. What are ways to improve my techniques for writing larger programs. I have written many demo programs learning some aspects of code wether it be dynamic binding or creating function and I understand most of that but how can I learn and employ techniques to make my programs better. I try to employ the lessons I learn in the programs that I write. Acclerated C++ is the best book I have...
3
1480
by: Ray | last post by:
OK, maybe I shoot a more general question to the group since there are so many great programmers here: how do you practice your craft? I do it in the following way: 1. Set aside 30 minutes to 1 hour a day to read up on the latest development, be it about the tool I'm using, the language, or the platform, or the framework, etc. 2. Once every 1-2 months, go to Amazon, and look for the book with the
7
1508
by: Michael | last post by:
Hey, I'm, I guess, an itermediate programmer and I have a question about learning any programming language. I understand that as a programmer you're going to probably constantly be re-writing code and the best method would be to save a template or other etc. I also know that it's ok to use other people's code to help you do something, but wouldn't it be best to re-type the code or study it until YOU learn how to do it and you actually...
7
4963
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C Programming Language Secure Coding Standard"
6
1344
by: Michael | last post by:
Hi, A quick and most likely simply question about which is generally better programming with PHP. Is it more proper to break out of PHP code to write HTML, or is it ok rto do it within print() statements? Such as... SCRENARIO A) <?php ....code... ?>
9
1901
by: Smithers | last post by:
Please consider this humble method: public void ResetCounters() { m_TotalExceptionsDetected = 0; m_TotalMessagesSent = 0; } Given no further information, would you wrap those two lines in a try... catch block?
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.