SqlTransaction

  • Thread starter Thread starter Swami Muthuvelu
  • Start date Start date
S

Swami Muthuvelu

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
 
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" <[email protected]>
!Sender: "Swami Muthuvelu" <[email protected]>
!Subject: SqlTransaction
!Date: Mon, 28 Jul 2003 16:51:22 -0700
!Lines: 38
!Message-ID: <[email protected]>
!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
!
!
!
!
!
!
!
!
 
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 the
SelectCommand of the DataAdapter before calling Update.
Then, the CommandBuilder will automatically enlist all
 
Hello Swami,

Yes, the codes are better now.

You could refer to KB artilce "HOW TO: Handle Database Transactions by Using the .NET Framework Without Using COM+
and MSDTC"

http://support.microsoft.com/?id=319665

Thanks very much.

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" <[email protected]>
!Sender: "Swami Muthuvelu" <[email protected]>
!References: <[email protected]> <U#[email protected]>
!Subject: RE: SqlTransaction
!Date: Wed, 30 Jul 2003 06:13:46 -0700
!Lines: 114
!Message-ID: <[email protected]>
!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: AcNWnGi1n1LzPKRPRw+tfLRcpXkIOw==
!Newsgroups: microsoft.public.dotnet.general
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:102833
!NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!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 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" <[email protected]>
!>!Sender: "Swami Muthuvelu" <[email protected]>
!>!Subject: SqlTransaction
!>!Date: Mon, 28 Jul 2003 16:51:22 -0700
!>!Lines: 38
!>!Message-ID: <[email protected]>
!>!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
!>!
!>!
!>!
!>!
!>!
!>!
!>!
!>!
!>
!>
!>.
!>
!
 
Back
Top