473,387 Members | 1,592 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,387 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 6135
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: 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...
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
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,...

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.