# MSSQL with MBS SQL plugin - Invalid cursor state

**URL:** <https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605>\
**Category:** Databases\
**Created:** [September 27, 2025, 10:35am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605 "2025-09-27T10:35:23Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [September 27, 2025, 10:35am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/1 "2025-09-27T10:35:23Z")

</div>

> [@Insert in MSSQL with MBS SQL plugin - Invalid cursor state](https://forum.xojo.com/t/insert-in-mssql-with-mbs-sql-plugin-invalid-cursor-state/43045):
>
> Hi, i’m trying to insert into a mssql database using SQLCommand MBS and get this error: 24000 [FreeTDS][SQL Server]Invalid cursor state The thing is that the record is successfully inserted in the database but I get an error. Also, a simple select in the database works as intended. I use this method to insert a record: [code] dim SQL as string = "Insert into “+me.kTableName+”(CT\_Num,CT\_Intitule,CT\_Type,CG\_NumPrinc,CT\_NumPayeur,N\_Risque,N\_CatTarif,N\_CatCompta,N\_Period,N\_Expedition,N\_Conditi…

Hi,

Scenario:  
Xojo 2025r2.1 web app  
MS SqlServer 2022  
MBS SQL plugin 25.3 (SQLConnectionMBS and SQLCommandMBS)

I have a similar problem, but with seemingly opposite behavior.

In several projects, even with many users connected, I have always used a single SQLConnectionMBS saved in the session, without any problems.

Now, in a new web project, if I use a single SQLConnectionMBS saved in the session, the error “24000 [FreeTDS][SQL Server]Invalid cursor state” does NOT occur ONLY if I set with.Option(‘AutoCache’) = “true”.

I don’t understand the reason for this error, given my past experience with a single SQLConnectionMBS.

On the other hand, if I create a new SQLConnectionMBS for each SQL statement, no error occurs regardless of the presence of AutoCache and its value.

What is the best solution, in your opinion?  
Use AutoCache = true (even with a single SQLConnectionMBS) or create a SQLConnectionMBS for each SQL statement (not using AutoCache, if not necessary)?

thank you.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 27, 2025, 12:07pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/2 "2025-09-27T12:07:39Z")

</div>

What do you do?

After a SQL Execute, you have a cursor in [SQLCommandMBS](https://www.monkeybreadsoftware.net/class-sqlcommandmbs.shtml), so you need to call FetchNext to go to the first row. If you do something else, the library my complain.

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [September 27, 2025, 2:40pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/3 "2025-09-27T14:40:34Z")

</div>

the runtime exception is on cmd.Execute

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 27, 2025, 3:08pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/4 "2025-09-27T15:08:17Z")

</div>

and?

What is before?

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [September 27, 2025, 3:19pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/5 "2025-09-27T15:19:54Z")

</div>

Dim con As SqlConnectionMBS = Session.db // option 1  
(or, if I create a new connection)  
Dim con As SqlConnectionMBS = GetDatabaseConnection() // option 2

dim sql As String = “SELECT …”

Dim cmd As New SqlCommandMBS(con, sql)  
cmd.Param(“name”).setAsString(pName)

cmd.Execute // here is the “24000 [FreeTDS][SQL Server]Invalid cursor state” error if I use  
// “option1”

If cmd.FetchNext Then  
…  
End If

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [September 28, 2025, 12:03pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/6 "2025-09-28T12:03:43Z")

</div>

On Web specifically, each Session _ **needs** _ it’s own database connection. You cannot have one single connection across the whole app. I wonder if the cursor state is a symptom of cross contaminated queries.

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [September 28, 2025, 1:40pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/7 "2025-09-28T13:40:45Z")

</div>

Thank you @Tim_Parnell, your message allows me to clarify:

In my previous projects, I always created one connection for each session.

What happens now (and what I don’t understand) is that I have to create a connection before executing each SQL statement and close the same connection at the end of the execution.

Or create a single connection per session, but with Option(‘AutoCache’) = “true.”

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 28, 2025, 4:37pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/8 "2025-09-28T16:37:01Z")

</div>

usually you have one connection per session for stuff happing in that session.

you may also have a separate global connection for stuff your app needs to do like logging exceptions.

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [September 29, 2025, 6:12am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/9 "2025-09-29T06:12:15Z")

</div>

thank you @Christian_Schmitz.

can you explain what happens using the AutoCache option?

can you confirm that using:

a) a single connection per session and

b) Option(“AutoCache”) = “true”

can solve my strange problem?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 29, 2025, 6:17am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/10 "2025-09-29T06:17:40Z")

</div>

Auto cache means that the plugin immediately after execution loads all records in memory on the client side.

And then your loop reads the cached values.

Somehow you may mess up the order of commands here like reading field before moving to first record.

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [October 21, 2025, 11:16am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/11 "2025-10-21T11:16:37Z")

</div>

I would like to update this discussion because my problem has not yet been resolved.  
To continue the development in a Mac Intel environment with FreeTDS, I have chosen to open and close an SqlConnectionMBS for each SQL statement executed.  
even though it seems absurd at first glance.  
Today I was able to run the application in a Windows environment with a single SqlConnectionMBS created when opening the web session.  
So, as I have always done.  
Unfortunately, at this time I cannot test the same application on Linux, but I have never had similar problems with other applications using FreeTDS on Linux.  
My question: if the problem only affects FreeTDS on MacOS, what is the solution?  
Thanks to anyone who can help.

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [October 24, 2025, 11:11am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/12 "2025-10-24T11:11:20Z")

</div>

Hi all,  
For me, it’s a FreeTDS error on macOS, since it works on Linux.  
Can anyone confirm or deny this opinion?  
If it is an error, can it be corrected? The MBS plugin documentation still states the access to SqlServer from Mac/Linux is via FreeTDS.  
So it is assumed it should work or, if there is a bug, I think it should be fixed.  
Alternatively, if the MBS plugin can also work with Microsoft’s ODBC driver for macOS, are there any examples or positive/negative experiences?  
Thank you.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 25, 2025, 8:33pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/13 "2025-10-25T20:33:25Z")

</div>

Well, last time I checked the FreeTDS library worked just fine.  
Other people use it and are happy.

Can you reproduce this with a small example project?

We may debug this, if you can reproduce it.  
The error just indicates that you called commands in wrong order.

---

<div class="post-metadata">

**Author:** ![Giacomo\_Bernardi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/9dc877/32.png) [@Giacomo\_Bernardi](https://forum.xojo.com/u/Giacomo_Bernardi)\
**Post date:** [October 27, 2025, 9:14pm UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/14 "2025-10-27T21:14:07Z")

</div>

It is the same application, and the execution order is the same whether the application is running on Windows (native ODBC)/macOS (FreeTDS for macOS)/Linux (FreeTDS for Linux).  
The cause of the problem is not the execution order.  
As I specified, the error probably only occurs when the application is running on macOS.  
This is especially true when more than one SQL command is executed in a method.  
The same application works fine on Windows and, I believe, also on Linux (based on past experience; I am currently unable to run it on Linux).  
Furthermore, I was asking if the SQL MBS plugin can use the MacOS/Linux version of Microsoft’s ODBC driver.  
And if it cannot use it, why not?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 28, 2025, 6:00am UTC](https://forum.xojo.com/t/mssql-with-mbs-sql-plugin-invalid-cursor-state/86605/15 "2025-10-28T06:00:37Z")

</div>

Can you reproduce this in a sample project.  
FreeTDS may react a bit different then ODBC, so there may still be an order issue, which is okay for one, but not the other.

Could you do this in a console project?  
And then add these lines to the top of the open event:

```auto
SQLDatabaseMBS.debugging = True
SQLGlobalsMBS.debugging = True

```

this will tell the plugin to log a ton of things.  
It should show up in the console app in the window and we’d love to know what you do before you get the error.
