473,473 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Urgent problem: Any help greatly appreciated

Hi everyone,

I'm having a problem that I don't know how to sort.

I am trying to execute a number of SQL stored procedures in a single
transaction. However it always throughs an exception saying that the "Thread
was being aborted"
I really need to be able to execute these procedures in a single transaction
so that the data doesnt become corrupt.

If anyone could help me I would be very greatful.

Sincerest thanks and kindest regards

Simon

public static bool executeBatchTransaction(ArrayList cmds){
SqlConnection con = new SqlConnection(connectionString);
IEnumerator cmdEnumerator;
SqlCommand currentCmd;
SqlTransaction trans;
DataSet dataset = new DataSet();

// Get this command seperately so we can start the transaction
currentCmd = (SqlCommand)cmds[0];

// We can't put this in a try block because if con.open fails, trans wont
be assigned to and we'll
// get an unassigned variable. Wont compile
// Start the transaction
currentCmd.Connection = con;
currentCmd.Connection.Open();
trans = currentCmd.Connection.BeginTransaction();
currentCmd.Transaction = trans;
// Execute the first command seperately
try{
if(!executeNonQuery(currentCmd)){
trans.Rollback();
return false;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeBatchTransaction(ArrayLi st cmds", e ));
MiscLogic.redirectOnError("/errors/defaultErrorPage.aspx");
}

cmdEnumerator = cmds.GetEnumerator();
// Skip the first command
cmdEnumerator.MoveNext();

while(cmdEnumerator.MoveNext()){
currentCmd = (SqlCommand)cmdEnumerator.Current;
try{
if(executeNonQuery(currentCmd)){
continue;
}
else{
trans.Rollback();
return false;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeBatchTransaction(ArrayLi st cmds", e ));
MiscLogic.redirectOnError("/errors/defaultErrorPage.aspx");
}
}
// If we get to here all the commands executed successfully. Commit and
return
trans.Commit();
return true;

}
Nov 16 '05 #1
3 1081
Can you combine those store procedures to derive a new one in your SQL
server?

chanmm
"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi everyone,

I'm having a problem that I don't know how to sort.

I am trying to execute a number of SQL stored procedures in a single
transaction. However it always throughs an exception saying that the "Thread was being aborted"
I really need to be able to execute these procedures in a single transaction so that the data doesnt become corrupt.

If anyone could help me I would be very greatful.

Sincerest thanks and kindest regards

Simon

public static bool executeBatchTransaction(ArrayList cmds){
SqlConnection con = new SqlConnection(connectionString);
IEnumerator cmdEnumerator;
SqlCommand currentCmd;
SqlTransaction trans;
DataSet dataset = new DataSet();

// Get this command seperately so we can start the transaction
currentCmd = (SqlCommand)cmds[0];

// We can't put this in a try block because if con.open fails, trans wont
be assigned to and we'll
// get an unassigned variable. Wont compile
// Start the transaction
currentCmd.Connection = con;
currentCmd.Connection.Open();
trans = currentCmd.Connection.BeginTransaction();
currentCmd.Transaction = trans;
// Execute the first command seperately
try{
if(!executeNonQuery(currentCmd)){
trans.Rollback();
return false;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeBatchTransaction(ArrayLi st cmds", e )); MiscLogic.redirectOnError("/errors/defaultErrorPage.aspx");
}

cmdEnumerator = cmds.GetEnumerator();
// Skip the first command
cmdEnumerator.MoveNext();

while(cmdEnumerator.MoveNext()){
currentCmd = (SqlCommand)cmdEnumerator.Current;
try{
if(executeNonQuery(currentCmd)){
continue;
}
else{
trans.Rollback();
return false;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeBatchTransaction(ArrayLi st cmds", e )); MiscLogic.redirectOnError("/errors/defaultErrorPage.aspx");
}
}
// If we get to here all the commands executed successfully. Commit and
return
trans.Commit();
return true;

}

Nov 16 '05 #2

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi everyone,

I'm having a problem that I don't know how to sort.

I am trying to execute a number of SQL stored procedures in a single
transaction. However it always throughs an exception saying that the "Thread was being aborted"
I really need to be able to execute these procedures in a single transaction so that the data doesnt become corrupt.

If anyone could help me I would be very greatful.

Sincerest thanks and kindest regards

OK, where to start.

First, don't return bool. Just use a void function. If anything goes wrong
throw an exception.

Second don't do a ASP.NET redirect in this function. It belongs in the
catch block of the code which invokes this function. Not only is is "tier
mixing", the redirect is implemented with a ThreadAbortException, and it can
be confusing to follow the thread of execution.

Third, you need to guaratee that the connection gets closed.

Forth, simplify the program flow.

Try this instead

public static SqlConnection connect()
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
return con;
}
public static void executeBatchTransaction(ArrayList cmds)
{
using (SqlConnection con = connect())
{
SqlTransaction trans = con.BeginTransaction();;
for (int i = 0; i < cmds.Count; i++)
{
SqlCommand cmd = (SqlCommand)cmds[i];
cmd.Transaction = trans;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
trans.Rollback();
throw;
}
}
trans.Commit();
}
}
David

Nov 16 '05 #3

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi everyone,

I'm having a problem that I don't know how to sort.

I am trying to execute a number of SQL stored procedures in a single
transaction. However it always throughs an exception saying that the "Thread
was being aborted"
I really need to be able to execute these procedures in a single

transaction
so that the data doesnt become corrupt.

If anyone could help me I would be very greatful.

Sincerest thanks and kindest regards

OK, where to start.

First, don't return bool. Just use a void function. If anything goes

wrong throw an exception.

Second don't do a ASP.NET redirect in this function. It belongs in the
catch block of the code which invokes this function. Not only is is "tier
mixing", the redirect is implemented with a ThreadAbortException, and it can be confusing to follow the thread of execution.

Third, you need to guaratee that the connection gets closed.

Forth, simplify the program flow.

Try this instead

public static SqlConnection connect()
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
return con;
}

You can shorten this even further, at the risk of being a tad cryptic, to

public static void executeBatchTransaction(ArrayList cmds)
{
using (SqlConnection con = connect())
using (SqlTransaction trans = con.BeginTransaction())
{
for (int i = 0; i < cmds.Count; i++)
{
SqlCommand cmd = (SqlCommand)cmds[i];
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
}
trans.Commit();
}
}

Since SqlTransaction.Dispose will rollback an uncommited transaction.

David
Nov 16 '05 #4

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

Similar topics

3
by: kieran | last post by:
Hi, I have a form which is submitting into a sql server db. i have one of the fields set to ntext which says length of 16. when i insert info, it only sumbits about a paragraph and when i pull...
3
by: Simon Harvey | last post by:
Hi everyone, I'm having a problem that I don't know how to sort. I am trying to execute a number of SQL stored procedures in a single transaction. However it always throughs an exception...
7
by: DJP | last post by:
Hi, I need to read a file programmatically until end of file. My logic is as follows: while(!feof(Fp)) { fgets(readLine,10000,Fp);
1
by: Chris Shaw | last post by:
Can anyone help. I have an access database with a couple of thousand records in it ... :shock: problem is i need to type one of the fields values into a data entry window for another...
3
by: Bill Nguyen | last post by:
I found that rows in datagrid can be sorted by clicking on the column header. However, this doesn't refresh the .currentindex DataGrid1.CurrentCell.RowNumber I need either to disable colum...
4
by: Jeff Cobelli | last post by:
I am running two sites on the same server, one for live clients and one for testing before we post to live. Each site contains both C# and VB.NET web services that connect to a SQL Server 2000...
4
by: JonathanParker | last post by:
Looking for some help urgently with some VB SQL Access stuff. You'll have to excuse the messiness of the code, I'm a newbie at all this. I'm running this code with a selection of check boxes to...
1
by: Domini | last post by:
Hi all, I need urgent help with an xml issue. Let me explain the scenario: My VB.NET app needs to read an xml (e.g. Sales Orders). This xml contains say 10 sales orders. Next the app needs to split...
2
by: samsalmanu | last post by:
I'm trying to ftp from a Unix machine to a Windows machine.. I've tried the following #!/bin/sh #set -x USER="user1" PASS="pass1" HOSTNAME="host1"
3
by: CaseT | last post by:
Hi, My company's website is down so I'm hoping someone has seen this bizarre problem/behavior before and can help me out .. We have a USA and an International site. Two directories under the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.