473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1583
On Nov 21, 4:34 pm, Julia B <Jul...@discussions.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(connectionString))
{
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = null;

try
{
// BeginTransaction() Requires Open Connection
connection.Open();

transaction = connection.BeginTransaction();

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

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

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

transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
}
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...@discussions.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(connectionString))
{
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = null;

try
{
// BeginTransaction() Requires Open Connection
connection.Open();

transaction = connection.BeginTransaction();

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

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

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

transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
}
Nov 21 '07 #3

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

Similar topics

8
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?...
23
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...
5
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
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...
20
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...
1
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...
6
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. ...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.