473,769 Members | 5,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design issue with a try....catch... .finally block

Hi, I'm wondering if anyone can give me any ideas/good practice/advice.

I've got a web form which a user inputs lots of data into, then presses
submit. The submit button uses two classes to input data into 2 tables. I
want to put a try....catch... finally block into the data input sections of
the classes or submit.on_click event to ensure that, if there's a problem
with data entry into the database, that the system doesn't crash and the
connection remains open & locked.

However, my dilemma is where do I put the try....catch... .finally? If I put
one in each of the subs in the two classes I have an issue if the second sub
fails. I want both or none of them to succeed, i.e. if the first one enters
data correctly and the second one fails, then the system crashes but I've got
a data problem.

If I put the try....catch... .finally in the webform, I've got the problem
that the connection objects are declared and used in the classes, so I could
not close or dispose of them from the webform (correct me if I'm wrong
here!!!).

So I'm a bit stuck. What's the best way to design something like this? Has
anyone any advice.

Thanks
Julia

Nov 21 '07 #1
2 1607
On Nov 21, 4:34 pm, Julia B <Jul...@discuss ions.microsoft. comwrote:
Hi, I'm wondering if anyone can give me any ideas/good practice/advice.

I've got a web form which a user inputs lots of data into, then presses
submit. The submit button uses two classes to input data into 2 tables. I
want to put a try....catch... finally block into the data input sections of
the classes or submit.on_click event to ensure that, if there's a problem
with data entry into the database, that the system doesn't crash and the
connection remains open & locked.

However, my dilemma is where do I put the try....catch... .finally? If I put
one in each of the subs in the two classes I have an issue if the second sub
fails. I want both or none of them to succeed, i.e. if the first one enters
data correctly and the second one fails, then the system crashes but I've got
a data problem.

If I put the try....catch... .finally in the webform, I've got the problem
that the connection objects are declared and used in the classes, so I could
not close or dispose of them from the webform (correct me if I'm wrong
here!!!).

So I'm a bit stuck. What's the best way to design something like this? Has
anyone any advice.

Thanks
Julia
What database are you using? SQL Server?

Use SQL Server Transactions

using (SqlConnection connection =
new SqlConnection(c onnectionString ))
{
SqlCommand command = connection.Crea teCommand();
SqlTransaction transaction = null;

try
{
// BeginTransactio n() Requires Open Connection
connection.Open ();

transaction = connection.Begi nTransaction();

// Assign Transaction to Command
command.Transac tion = transaction;

// Execute 1st sub using the opened Command
command.Command Text = "Insert ...";
command.Execute NonQuery();

// Execute 2nd sub using the same Command
command.Command Text = "Update..." ;
command.Execute NonQuery();

transaction.Com mit();
}
catch
{
transaction.Rol lback();
throw;
}
finally
{
connection.Clos e();
}
}
Nov 21 '07 #2
Really sorry, I should have been more specific. I'm using Microsoft Access.

Julia

"Alexey Smirnov" wrote:
On Nov 21, 4:34 pm, Julia B <Jul...@discuss ions.microsoft. comwrote:
Hi, I'm wondering if anyone can give me any ideas/good practice/advice.

I've got a web form which a user inputs lots of data into, then presses
submit. The submit button uses two classes to input data into 2 tables. I
want to put a try....catch... finally block into the data input sections of
the classes or submit.on_click event to ensure that, if there's a problem
with data entry into the database, that the system doesn't crash and the
connection remains open & locked.

However, my dilemma is where do I put the try....catch... .finally? If I put
one in each of the subs in the two classes I have an issue if the second sub
fails. I want both or none of them to succeed, i.e. if the first one enters
data correctly and the second one fails, then the system crashes but I've got
a data problem.

If I put the try....catch... .finally in the webform, I've got the problem
that the connection objects are declared and used in the classes, so I could
not close or dispose of them from the webform (correct me if I'm wrong
here!!!).

So I'm a bit stuck. What's the best way to design something like this? Has
anyone any advice.

Thanks
Julia

What database are you using? SQL Server?

Use SQL Server Transactions

using (SqlConnection connection =
new SqlConnection(c onnectionString ))
{
SqlCommand command = connection.Crea teCommand();
SqlTransaction transaction = null;

try
{
// BeginTransactio n() Requires Open Connection
connection.Open ();

transaction = connection.Begi nTransaction();

// Assign Transaction to Command
command.Transac tion = transaction;

// Execute 1st sub using the opened Command
command.Command Text = "Insert ...";
command.Execute NonQuery();

// Execute 2nd sub using the same Command
command.Command Text = "Update..." ;
command.Execute NonQuery();

transaction.Com mit();
}
catch
{
transaction.Rol lback();
throw;
}
finally
{
connection.Clos e();
}
}
Nov 21 '07 #3

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

Similar topics

8
2737
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block? (ie, forget the finally block and just end the try/catch and put the code after the try/catch block). Or does the "finally" construct add some additional functionality?
23
3083
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
5
1673
by: Neelam Karne | last post by:
I am using VB.Net with .Net Framework 1.1 Here is my code Try 'Try my statements Catch
18
2891
by: Simon | last post by:
I was of the impression that code placed after a Try...Catch block was only executed if there was no exception thrown. I've got some VB.net code as part of a Windows form that executes even when an exception is thrown - it behaves as if the code is part of a finally block. Looking through all the documentation and MSDN articles, it seems that none of the examples contain code placed
20
1379
by: Woody Splawn | last post by:
In a message yesterday titled Try Catch Question I got numerous responses. Thank you to all. After all the smoke clears I guess the question is where to place the Return True statement in the block of code below that is part of a function. Should it be at the end of the try block, as dipicted below, or should it be after the End Try Block? Or does it make a difference? Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select...
1
1872
by: GS | last post by:
Any points of what would be the good error handling design for application? User error handling in Application_OnError and throw() new errors on conditions through the code? I'd like utlimiately to consolidate all error_handling in one method which I'll be able to easily modify to write to event log or text file etc instead of error hanlding scattered through the code. Thanks, GS
6
7450
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. The question is: after the ThreadAbortException is thrown, does the childThread continue run the remaining code in the catch block?
5
2320
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try Dim cmd As New SqlCommand cmd.CommandText = "spSetAdLinks" cmd.CommandType = Data.CommandType.StoredProcedure cmd.Connection = sqlCon
0
9587
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
10211
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...
0
9863
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
8870
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...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.