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

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;

}
Jul 21 '05 #1
3 1584
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;

}

Jul 21 '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

Jul 21 '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
Jul 21 '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...
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: 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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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...

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.