Solved

USING SQL TRANSACTIONS IN VB.NET

Posted on 2004-10-19
3
584 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do sunrise and sunset times change with altitude 14 46
Very Large data in MYSQL 7 94
Vb.net threads keep increasing 2 50
Web page design problem 3 17
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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