tursodatabase/turso · error · InvalidOperationException
Connection must be set before executing a command.
Error message
Connection must be set before executing a command.
What it means
The synchronous Execute path requires a connection: it either delegates to the remote client or calls Prepare() against the native handle (TursoCommand.cs:268-271). A null _connection at entry throws InvalidOperationException before any SQL is touched.
Source
Thrown at bindings/dotnet/src/Turso.Data/TursoCommand.cs:277
}
return sql;
}
private static bool ParsePragmaEnabled(string value)
{
value = value.Trim('\'', '"');
return long.TryParse(value, out var number)
? number != 0
: value.Equals("ON", StringComparison.OrdinalIgnoreCase)
|| value.Equals("TRUE", StringComparison.OrdinalIgnoreCase)
|| value.Equals("YES", StringComparison.OrdinalIgnoreCase);
}
private DbDataReader Execute(CommandBehavior behavior = CommandBehavior.Default)
{
if (_connection is null)
throw new InvalidOperationException("Connection must be set before executing a command.");
if (_connection.IsRemote)
return ExecuteRemoteAsync(behavior, CancellationToken.None).GetAwaiter().GetResult();
IDisposable? syncOperation = _connection.EnterSyncOperation();
try
{
PrepareCore();
var statement = _statement ?? throw new InvalidOperationException("Command was not prepared.");
_statement = null;
try
{
var reader = new TursoDataReader(this, statement, behavior, syncOperation);
syncOperation = null;
return reader;
}
catchView on GitHub (pinned to c1e5928725)
Solutions
- Assign the connection before executing: cmd.Connection = conn; (after conn.Open()).
- Create commands via the connection: using var cmd = conn.CreateCommand();
- In shared helpers, null-check cmd.Connection and fail with your own clearer error message.
Example fix
// before
var cmd = new TursoCommand("SELECT 1");
using var reader = cmd.ExecuteReader(); // no Connection -> throws
// after
using var conn = new TursoConnection(cs);
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT 1";
using var reader = cmd.ExecuteReader(); Defensive patterns
Strategy: validation
Validate before calling
if (cmd.Connection is not TursoConnection)
throw new InvalidOperationException("Bind an open TursoConnection to the command before executing."); Prevention
- Use conn.CreateCommand() everywhere instead of new TursoCommand().
- Open the connection before the first execute.
- In generic code, check for a null Connection and surface a descriptive error.
When it happens
Trigger: var cmd = new TursoCommand("SELECT 1"); cmd.ExecuteReader(); — ExecuteReader/ExecuteNonQuery/ExecuteScalar on a command whose Connection was never assigned or was set to null.
Common situations: Forgetting conn.CreateCommand() and newing the command directly; a command created in one scope and reused after its connection was unbound; unit tests exercising commands without a live connection.
Related errors
- Connection must be a TursoConnection.
- Connection must be set before preparing a command.
- The connection is already open.
- Turso database is closed.
- Turso batch execution is currently supported only for remote
AI-assisted analysis of tursodatabase/turso@c1e5928725 (2026-08-20).
Data as JSON: /api/errors/5d3ede63386dbf5b.
Report an issue: GitHub.