473,405 Members | 2,210 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,405 software developers and data experts.

try and catch in a Transaction

Hi,

Would someone tell me why errors happen in TestCommand.ExecuteNonQuery and
the application just hang in there, it won't go to the catch. Why?
Thanks for help.
Jason

try
{
TestCommand[i].Transaction=myTrans;
TestCommand[i].ExecuteNonQuery();
}
catch (SqlException EQry)
{
MessageBox.Show(String.Format("An exception occurred" +
" : {0}. Please contact your system administrator.", EQry.Message));
}
May 15 '06 #1
2 2799
Hi Jason,

Currently, the catch operator catches exceptions of type
System.Data.SqlClient.SqlException that occur in the
SqlCommand.ExecuteNonQuery() call [1]. Most likely the exception that is
thrown (and unhandled) is not of the aforementioned type.

You can append another catch statement for handling exceptions of type
System.InvalidOperationException and/or System.Exception, because they can be
thrown in certain circumstances, too.

[1] SqlCommand Documentation:
http://msdn2.microsoft.com/en-us/library/z4956bkc.aspx

--
Stanimir Stoyanov
www.stoyanoff.info
"Jason Huang" wrote:
Hi,

Would someone tell me why errors happen in TestCommand.ExecuteNonQuery and
the application just hang in there, it won't go to the catch. Why?
Thanks for help.
Jason

try
{
TestCommand[i].Transaction=myTrans;
TestCommand[i].ExecuteNonQuery();
}
catch (SqlException EQry)
{
MessageBox.Show(String.Format("An exception occurred" +
" : {0}. Please contact your system administrator.", EQry.Message));
}

May 15 '06 #2
Thanks Stanimir.
I now also have the System.InvalidOperationException and System.Exception on
the catch,
and it is System.FormaException shows up on the MessageBox.
But is it possible to know what data/field brings out the error?
Thanks for help.
Jason

Hi Jason,

Currently, the catch operator catches exceptions of type
System.Data.SqlClient.SqlException that occur in the
SqlCommand.ExecuteNonQuery() call [1]. Most likely the exception that is
thrown (and unhandled) is not of the aforementioned type.

You can append another catch statement for handling exceptions of type
System.InvalidOperationException and/or System.Exception, because they can
be
thrown in certain circumstances, too.

[1] SqlCommand Documentation:
http://msdn2.microsoft.com/en-us/library/z4956bkc.aspx

--
Stanimir Stoyanov
www.stoyanoff.info
"Jason Huang" wrote:
Hi,

Would someone tell me why errors happen in TestCommand.ExecuteNonQuery
and
the application just hang in there, it won't go to the catch. Why?
Thanks for help.
Jason

try
{
TestCommand[i].Transaction=myTrans;
TestCommand[i].ExecuteNonQuery();
}
catch (SqlException EQry)
{
MessageBox.Show(String.Format("An exception occurred" +
" : {0}. Please contact your system administrator.",
EQry.Message));
}

May 16 '06 #3

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

Similar topics

6
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
4
by: Big D | last post by:
Hi all, I'm trying to understand the best way to use a try catch with a SQL transaction. I have a number of sql statements that need to be run, such as: 'open sqlConnection, get command...
2
by: JenHu | last post by:
Hi experts, I want to rollback the insert/update in my SQL Server database tables if there's any errors, and I use trans.Rollback(). However, whenever error generates, the rollback doesn't work...
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
26
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
3
by: Michael C | last post by:
I'm writing an app on the PDA using C# with .net 1.1. It is all working well except in some cases a try catch is simply ignored and a totally different error is returned. I've got code like below...
2
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
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...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...
0
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...

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.