473,399 Members | 4,177 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,399 software developers and data experts.

SqlTransaction

Hi,
I using command builder to generate my insert, update and
delete statements..something like,

data_adapter = New SqlClient.SqlDataAdapter(SQL,
ActiveConnection)

command_builder = New SqlClient.SqlCommandBuilder
(data_adapter)

After filling a data grid and making changes to the data,
I want to call the adapter.Update method in a database
transaction, so that I can rollback the transaction, if
the update fails..
This is my code, for that..
***************************************
Dim trans As SqlClient.SqlTransaction
trans = adp.SelectCommand.Connection.BeginTransaction
adp.Update(ds)
***************
In the adp.Update(ds) line, I get the exception,
"Execute requires the command to have a transaction object
when the connection assigned to the command is in a
pending local transaction. The Transaction property of
the command has not been initialized."

What am I doing wrong..?
Please help with a code snippet.

Regards,
Swami



Jul 19 '05 #1
2 6137
Hello Swami,

When you are using a CommandBuilder and you want to use transactions, you must associate the transaction with the
SelectCommand of the DataAdapter before calling Update. Then, the CommandBuilder will automatically enlist all
generated commands in the transaction when you call Update.

For an example:
data_adapterSelectcommand.Transaction = trans;
data_adapter.InsertCommand.Transaction = trans;
data_adapter.UpdateCommand.Transaction = trans;
data_adapter.DeleteCommand.Transaction = trans;

Hope it helps.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "Swami Muthuvelu" <sw***@mclsystems.com>
!Sender: "Swami Muthuvelu" <sw***@mclsystems.com>
!Subject: SqlTransaction
!Date: Mon, 28 Jul 2003 16:51:22 -0700
!Lines: 38
!Message-ID: <04****************************@phx.gbl>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcNVYyXY+PcCw/X4Qo2zrO1QD6xvrA==
!Newsgroups: microsoft.public.dotnet.general
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:102645
!NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Hi,
!I using command builder to generate my insert, update and
!delete statements..something like,
!
!data_adapter = New SqlClient.SqlDataAdapter(SQL,
!ActiveConnection)
!
!command_builder = New SqlClient.SqlCommandBuilder
!(data_adapter)
!
!After filling a data grid and making changes to the data,
!I want to call the adapter.Update method in a database
!transaction, so that I can rollback the transaction, if
!the update fails..
!This is my code, for that..
!***************************************
!Dim trans As SqlClient.SqlTransaction
!trans = adp.SelectCommand.Connection.BeginTransaction
!adp.Update(ds)
!***************
!In the adp.Update(ds) line, I get the exception,
!"Execute requires the command to have a transaction object
!when the connection assigned to the command is in a
!pending local transaction. The Transaction property of
!the command has not been initialized."
!
!What am I doing wrong..?
!Please help with a code snippet.
!
!Regards,
!Swami
!
!
!
!
!
!
!
!
Jul 19 '05 #2
Yanhong,
Thanks for your reply.
Can you please also tell me how to define the transaction
object?. Should it be like this?

Dim trans As SqlClient.SqlTransaction
trans = adp.SelectCommand.Connection.BeginTransaction

and then,

data_adapterSelectcommand.Transaction = trans
data_adapter.InsertCommand.Transaction = trans
data_adapter.UpdateCommand.Transaction = trans
data_adapter.DeleteCommand.Transaction = trans
?.

Please help me on this.
Regards,
Swami
-----Original Message-----
Hello Swami,

When you are using a CommandBuilder and you want to use transactions, you must associate the transaction with theSelectCommand of the DataAdapter before calling Update. Then, the CommandBuilder will automatically enlist allgenerated commands in the transaction when you call Update.
For an example:
data_adapterSelectcommand.Transaction = trans;
data_adapter.InsertCommand.Transaction = trans;
data_adapter.UpdateCommand.Transaction = trans;
data_adapter.DeleteCommand.Transaction = trans;

Hope it helps.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
!Content-Class: urn:content-classes:message
!From: "Swami Muthuvelu" <sw***@mclsystems.com>
!Sender: "Swami Muthuvelu" <sw***@mclsystems.com>
!Subject: SqlTransaction
!Date: Mon, 28 Jul 2003 16:51:22 -0700
!Lines: 38
!Message-ID: <04****************************@phx.gbl>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcNVYyXY+PcCw/X4Qo2zrO1QD6xvrA==
!Newsgroups: microsoft.public.dotnet.general
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:102645!NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Hi,
!I using command builder to generate my insert, update and!delete statements..something like,
!
!data_adapter = New SqlClient.SqlDataAdapter(SQL,
!ActiveConnection)
!
!command_builder = New SqlClient.SqlCommandBuilder
!(data_adapter)
!
!After filling a data grid and making changes to the data,!I want to call the adapter.Update method in a database
!transaction, so that I can rollback the transaction, if
!the update fails..
!This is my code, for that..
!***************************************
!Dim trans As SqlClient.SqlTransaction
!trans = adp.SelectCommand.Connection.BeginTransaction
!adp.Update(ds)
!***************
!In the adp.Update(ds) line, I get the exception,
!"Execute requires the command to have a transaction object!when the connection assigned to the command is in a
!pending local transaction. The Transaction property of
!the command has not been initialized."
!
!What am I doing wrong..?
!Please help with a code snippet.
!
!Regards,
!Swami
!
!
!
!
!
!
!
!
.

Jul 19 '05 #3

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

Similar topics

2
by: mahajan.sanjeev | last post by:
I have two SQLConnection objects having the same connection string and two corresponding SQLCommand objects for each connection object. I am using SQLTransaction with the first SQLConnection object...
2
by: mahajan.sanjeev | last post by:
Hi, I am having problems with rollback using the SQLTransaction object. I am trying to insert records in two tables in a transaction. I want to rollback all the changes if any exception occurs...
0
by: mahajan.sanjeev | last post by:
Hi All, I am using a SQLTransaction in a .Net application to insert records into a SQL Server table. At one time, there are 5000 or more records to be inserted one by one. It takes some 20-25...
0
by: perspolis | last post by:
Hi all I used SqlTransaction inmy application.. SqlTransaction transact=sqlConnection1.BeginTransaction(IsoLationLevel.Something); sqlSelect.Transaction=transact; sqlInsert.Transaction=transact;...
3
by: Junyin.Wu | last post by:
what is difference between SqlTransaction and T-Sql(begin tran, commit tran, rollback tran) thanks.
0
by: Joe Rigley | last post by:
Hi All, I am using a SqlTransaction object to process a group of database insert / update statements on Sql Server 200 SP4 to complete a business process. Directly after the Commit method is...
5
by: Swami Muthuvelu | last post by:
Hi, I using command builder to generate my insert, update and delete statements..something like, data_adapter = New SqlClient.SqlDataAdapter(SQL, ActiveConnection) command_builder = New...
3
by: Neven Klofutar | last post by:
Hi, I'm trying to retrieve some information from DB using SqlTransaction, but I have a problem. I'm executing some DELETE SQL statements using Transaction. Then later on (because of the lousy...
4
by: perspolis | last post by:
Hi all I have a master-detail tables in sql server. I use SqlTransaction to insert data into both tables. it works well but if I check the "Cascade Delete Related Records" option in it's realtion...
0
by: buttslapper | last post by:
Hi, Recently we discovered in our production server this kind of exception : We are wondering what causes the transaction to be zombied, and why do we get a nullreferenceexception in this...
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: 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?
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
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
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.