473,394 Members | 1,658 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,394 software developers and data experts.

Problem with SqlCommand and T-SQL transaction

I'm having trouble with what should be a simple task; beginning and
committing T-SQL transactions using the SQLClient. I'm using a SqlCommand
(cmd) to begin the transaction and delete records from two tables. if both
records are successfully deleted, I'd like to commit the transaction.
Everything works fine until I attempt to commit the first time the foreach
loop runs, at this point I get the following exception. I haven't been able
to make sense of what I've seen on msdn or other newsgroups; this seems like
it should be an easy process since I'm creating a new SqlCommand every time
the foreach loop runs.

Any help or suggestions would be much appreciated.

Thanks, Andre
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK
TRANSACTION statement is missing. Previous count = 1, current count = 0.
foreach (DataRow dsRow in Class1.myDS.MasterAccounts.Rows)
{
//begin SQL Transaction
sSQL = "BEGIN TRAN;";
Class1.cmd = new SqlCommand(sSQL, Class1.cn);
Class1.cmd.CommandTimeout = 0;
Class1.cmd.ExecuteNonQuery();

//declare @ServiceID param.
Class1.cmd.Parameters.Add("@ServiceID", SqlDbType.Int, 4).Value =
Convert.ToInt32(dsRow["ServiceID"]);

sSQL = "DELETE FROM tblMainServices " +
"WHERE ServiceID = @ServiceID AND Service LIKE '920%';";
Class1.cmd.CommandText = sSQL;
Class1.cmd.ExecuteNonQuery();

sSQL = "DELETE FROM tblMainServices_B " +
"WHERE ServiceID = @ServiceID;";
Class1.cmd.CommandText = sSQL;
Class1.cmd.ExecuteNonQuery();

//Commit SQL Transaction
sSQL = "COMMIT TRAN;";
Class1.cmd.CommandText = sSQL;
Class1.cmd.ExecuteNonQuery();

iCount ++;

int iProgValue = Convert.ToInt16(20 * Convert.ToSingle(iCount / iRecords));
if (iProgValue != progBar.Value)
{
progBar.Value = iProgValue;
this.Update();}
}
Nov 16 '05 #1
2 8307

"Andre Ranieri" <An**********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
I'm having trouble with what should be a simple task; beginning and
committing T-SQL transactions using the SQLClient. I'm using a SqlCommand
(cmd) to begin the transaction and delete records from two tables. if
both
records are successfully deleted, I'd like to commit the transaction.
Everything works fine until I attempt to commit the first time the foreach
loop runs, at this point I get the following exception. I haven't been
able
to make sense of what I've seen on msdn or other newsgroups; this seems
like
it should be an easy process since I'm creating a new SqlCommand every
time
the foreach loop runs.

Any help or suggestions would be much appreciated.

Thanks, Andre


Try this instead:

public static SqlConnection Connect()
{
SqlConnection con = new SqlConnection("..");
con.Open();
return con;
}

......
using (SqlConnection con = Connect())
foreach (DataRow dsRow in Class1.myDS.MasterAccounts.Rows)
{
using (SqlTransaction tran = con.BeginTransaction() )
{
SqlCommand cmd1 = new SqlCommand( "DELETE FROM tblMainServices " +
"WHERE ServiceID = @ServiceID AND Service LIKE '920%';",con,tran);
cmd1.Parameters.Add("@ServiceID", SqlDbType.Int, 4).Value =
Convert.ToInt32(dsRow["ServiceID"]);

SqlCommand cmd2 = new SqlCommand( "DELETE FROM tblMainServices_B " +
"WHERE ServiceID = @ServiceID AND Service LIKE '920%';",con,tran);
cmd2.Parameters.Add("@ServiceID", SqlDbType.Int, 4).Value =
Convert.ToInt32(dsRow["ServiceID"]);

cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
tran.Commit();
iCount ++;
//...
}
}
David
Nov 16 '05 #2
David,

I wanted to thank you for your reply, I was able to get the code working
based on your feedback.

I'm still not sure why my T-SQL transactions didn't work and why the ADO.NET
tran did.
Nov 16 '05 #3

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

Similar topics

0
by: Bob | last post by:
I have an ASP.NET web application that has been running without any problems for a while. I recently transferred the site to shared hosting and had multiple users start to use the site. The problem...
3
by: mattgcon | last post by:
When I use a stored procedure to insert data into a table and when the Datagrid refreshes two new columns are added. The dataSet is set up with one table with NO columns defined. Please help me on...
4
by: mattgcon | last post by:
When I use a stored procedure to insert data into a table and when the Datagrid refreshes two new columns are added. The dataSet is set up with one table with NO columns defined. Please help me on...
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
1
by: Sunil Sabir | last post by:
Dear All, I have a search form which has 3 drop down boxes dropID ,dropPostCode, dropName. when I select some thing in the dropID name lets say 0001 I press go button.It displays a Record of...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
2
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
3
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
3
by: kpeeroo | last post by:
Private Function AddCompanyOvertime() As Integer Dim companyID As Integer = GetCompanyID() Console.WriteLine(companyID) Dim paramCompanyID As New SqlParameter("@CompanyID",...
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
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?
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
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
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...

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.