Solved

USING SQL TRANSACTIONS IN VB.NET

Posted on 2004-10-19
3
577 Views
Last Modified: 2012-06-22
Hi everyone,

I'm using VB.NET  and I have long standing problem that I really need a solution to concerning inserting and updating to a SQL database using transactions. Sample code from my save routine is as follows


'Create and open the connection
Dim tsql As String = "server=MYSERSVER;uid=ME;" + _
                        "pwd=MYPASS;database=MYDB;Connect Timeout=5;pooling=false"
Dim sqlconn As New SqlClient.SqlConnection
sqlconn = New SqlClient.SqlConnection(tsql)
sqlconn.Open()

'Create transaction
Dim sqltx As SqlClient.SqlTransaction
sqltx = sqlconn.BeginTransaction

'Specify the command to run
Dim sqlcomd As New SqlClient.SqlCommand
Dim sql as string = "INSERT INTO wherever"

'Add command to transaction
sqlcomd.Connection = sqlconn
sqlcomd.Transaction = sqltx
sqlcomd.CommandText = (sql)
pbrid = sqlcomd.ExecuteScalar()

---------------------------------
Thats the first part of the update - next I loop through an array and add
sql statements to the transaction, as follows

For i = 0 to totalrows
     Dim sql1 as string = "INSERT INTO WHERERVER (brid, pcid) VALUES (pbrid, i)"
     sqlcomd.Connection = sqlconn
     sqlcomd.Transaction = sqltx
     sqlcomd.CommandText = (sql1)
     sqlcomd.ExecuteNonQuery()
next

'Finally, I commit the whole thing
sqltx.Commit()
sqlconn.Close()
sqltx = Nothing
sqlconn = Nothing

----------------------------------------------------------------
The problem I am having is that occasionally an error occurs. The error is either 'ExecuteNonQuery: Connection property has not been initialised' OR
'SQL Transaction has completed; it is no longer usuable'. I have never been able to reproduce this error myself (but users have reported it) so i'm not sure exactly where in the code the error is occuring, I would assume as the sqltx.commit line. Fortunately, on the majority of occasions the save is still successfull but occasionally it isn't, which makes it even harder to find out where it is occuring.

Any help would be great.
0
Comment
Question by:hendrix500
3 Comments
 
LVL 10

Accepted Solution

by:
123654789987 earned 250 total points
ID: 12345627
It looks like the connection timeout  problem.

Why are u repeating this statement

 sqlcomd.Transaction = sqltx

Since u are using the same command object sqlcomd , in the for loop, I don't think it is needed.

U should have a try-catch block wherever the query is executed so that u can catch SqlExceptions. In that case u have to abort the transaction
0
 
LVL 1

Author Comment

by:hendrix500
ID: 12346937
Yeah I take you point about repeating the statement. Not sure if it's causing the problem or not but i'll take it out anyway.

As I mentioned the problem only happens occasionally so I will have to monitor any potential fixes for a couple of weeks before accespting an answer.

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now