Link to home
Start Free TrialLog in
Avatar of mattgoolsby
mattgoolsby

asked on

Transaction context in use by another session. Error

I have an ASP page that creates a SQL statement in on the fly and puts it in a string variable.  I then fill a recordset with the following code:

set rst = conCRep.Execute(sql)

The recordset is filled with about 750 records, and there is only one field per record.  At this point, I loop through the recordset as follows:

Do While Not rst.Eof

     sql = "SELECT * FROM BankComp WHERE "
     sql = sql & "Program = '" & rst("ProgramNumber") & "' AND EffectiveDate = '" & 
             EffDate & "'"

    set rstTest = conCRep.Execute(sql)

     .....

     rst.MoveNext
Loop

The first time it tries to run the rstTest statement, I get the "Transaction context in use by another session." error.  Any ideas what is going wrong?  This only seems to happen when the initial SQL statement is returning a lot of records.  For the record I am running SQL Server 2000.
Avatar of CJ_S
CJ_S
Flag of Netherlands image

What Lock are you using? Can you show the code you use to actually access the database?
Avatar of mattgoolsby
mattgoolsby

ASKER

I simply am opening a connection to the DB at the top of the ASP page with this code:

Set conCRep = Server.CreateObject("ADODB.Connection")
conCRep.Open ConnectionString

The ConnectionString variable is as follows:

ConnectionString = "provider=SQLOLEDB;Data Source=(local);Initial Catalog=CoverdellReporting;User ID=xxxxxxxx;Password=xxxxxxxx"
Avatar of namasi_navaretnam
You may need to this as follows

'This may need to be outside the loop
sql = "SELECT * FROM BankComp WHERE "
     sql = sql & "Program = '" & rst("ProgramNumber") & "' AND EffectiveDate = '" & 
             EffDate & "'"
set rstTest = conCRep.Execute(sql)

' The sql here may return more than one row. Plus, without closing the current result set, news rows are retrieved into it.

Do While Not rst.Eof


     rst.MoveNext
Loop
I do not think that is my problem.  In this particular case, the "set rst" statement returns 760 rows.  Each row has a ProgramNumber field and that's it.

I then need to loop through each of the 760 records.  At this point, I create the second SQL statement (set rstTest), which has to be inside the loop since it is using the ProgramNumber value.  "Program" and "EffectiveDate" is the primary key in the BankComp table, so only one row will ever be returned.

I ran my program where only 14 records were returned in the initial SQL statement, and it ran fine.  It seems to have a problem when it is large amounts of data.
Actually, I did not notice that there are different result sets.

Try this,

Do While Not rst.Eof

     sql = "SELECT * FROM BankComp WHERE "
     sql = sql & "Program = '" & rst("ProgramNumber") & "' AND EffectiveDate = '" & 
             EffDate & "'"

    set rstTest = conCRep.Execute(sql)
    'Extract infomation you need.
     restTest.Close()
     .....

     rst.MoveNext
Loop
The code errors out the first time through the "Do" loop.  As soon as it tries to run "set rstTest" for the first time is when I get the error.

Just to be sure, I added the "Close" statement, and that did not work.  It never even reaches that point before the error.
I have found a temporary workaround.  I am putting the ProgramNumber values into an array.  Instead of a "Do" loop, I use a "For" loop to loop through each value of the array, build my second SQL statement, and any other processing I need done.  This is working fine.

However, I would still like to know the source of my initial problem so I can avoid it in the future.
ASKER CERTIFIED SOLUTION
Avatar of namasi_navaretnam
namasi_navaretnam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial