tursodatabase/turso · error · InvalidOperationException
CommandText must be set before executing a command.
Error message
CommandText must be set before executing a command.
What it means
The remote execution path validates CommandText before doing any network work: null, empty, or whitespace-only SQL throws InvalidOperationException (TursoCommand.cs:288-289) instead of wasting a round-trip to the remote server.
Source
Thrown at bindings/dotnet/src/Turso.Data/TursoCommand.cs:312
}
catch
{
statement.Dispose();
throw;
}
}
finally
{
syncOperation?.Dispose();
}
}
private async Task<DbDataReader> ExecuteRemoteAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
if (_connection is null)
throw new InvalidOperationException("Connection must be set before executing a command.");
if (string.IsNullOrWhiteSpace(CommandText))
throw new InvalidOperationException("CommandText must be set before executing a command.");
ValidateTransaction();
cancellationToken.ThrowIfCancellationRequested();
var sql = RewriteFacadePragmas(CommandText, _connection);
var result = await _connection
.ExecuteRemoteAsync(sql, _parameterCollection, wantRows: true, CommandTimeout, cancellationToken)
.ConfigureAwait(false);
return new TursoRemoteDataReader(this, result, behavior);
}
private async Task<int> ExecuteRemoteNonQueryAsync(CancellationToken cancellationToken)
{
if (_connection is null)
throw new InvalidOperationException("Connection must be set before executing a command.");
if (string.IsNullOrWhiteSpace(CommandText))
throw new InvalidOperationException("CommandText must be set before executing a command.");
ValidateTransaction();View on GitHub (pinned to c1e5928725)
Solutions
- Guard remote calls: if (string.IsNullOrWhiteSpace(cmd.CommandText)) throw early with context.
- Set the SQL at construction: new TursoCommand(sql, conn).
- Fail fast at startup if required SQL/config strings are empty instead of discovering it mid-request.
Example fix
// before
cmd.CommandText = config.OptionalSql; // null in this env
await cmd.ExecuteNonQueryAsync(); // remote path -> throws
// after
cmd.CommandText = config.OptionalSql;
if (!string.IsNullOrWhiteSpace(cmd.CommandText))
await cmd.ExecuteNonQueryAsync(); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(cmd.CommandText))
throw new InvalidOperationException("CommandText is empty; refusing to call the remote endpoint."); Prevention
- Set CommandText via the constructor for remote commands.
- Validate SQL loaded from configuration at startup.
- Skip the execute call when optional SQL resolves empty.
When it happens
Trigger: ExecuteReaderAsync()/ExecuteNonQueryAsync() on a remote connection with cmd.CommandText never set, blank, or whitespace — e.g. SQL loaded from configuration that resolved to an empty string.
Common situations: SQL built from appsettings/environment values that are empty in one environment; conditional assignment skipped on a rarely-hit branch; a reused command whose CommandText was cleared between runs.
Related errors
- Turso batch execution is currently supported only for remote
- CommandText must be set before preparing a command.
- Parameter {parameter.ParameterName} was not found in the SQL
- TursoBatchCommand only supports CommandType.Text.
- Batch command must be a TursoBatchCommand.
AI-assisted analysis of tursodatabase/turso@c1e5928725 (2026-08-20).
Data as JSON: /api/errors/2f419026ba2ecbcc.
Report an issue: GitHub.