{"record":{"id":"2f419026ba2ecbcc","repo":"tursodatabase/turso","slug":"commandtext-must-be-set-before-executing-a-command","errorCode":null,"errorMessage":"CommandText must be set before executing a command.","messagePattern":"CommandText must be set before executing a command\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoCommand.cs","lineNumber":312,"sourceCode":"            }\n            catch\n            {\n                statement.Dispose();\n                throw;\n            }\n        }\n        finally\n        {\n            syncOperation?.Dispose();\n        }\n    }\n\n    private async Task<DbDataReader> ExecuteRemoteAsync(CommandBehavior behavior, CancellationToken cancellationToken)\n    {\n        if (_connection is null)\n            throw new InvalidOperationException(\"Connection must be set before executing a command.\");\n        if (string.IsNullOrWhiteSpace(CommandText))\n            throw new InvalidOperationException(\"CommandText must be set before executing a command.\");\n        ValidateTransaction();\n\n        cancellationToken.ThrowIfCancellationRequested();\n\n        var sql = RewriteFacadePragmas(CommandText, _connection);\n        var result = await _connection\n            .ExecuteRemoteAsync(sql, _parameterCollection, wantRows: true, CommandTimeout, cancellationToken)\n            .ConfigureAwait(false);\n        return new TursoRemoteDataReader(this, result, behavior);\n    }\n\n    private async Task<int> ExecuteRemoteNonQueryAsync(CancellationToken cancellationToken)\n    {\n        if (_connection is null)\n            throw new InvalidOperationException(\"Connection must be set before executing a command.\");\n        if (string.IsNullOrWhiteSpace(CommandText))\n            throw new InvalidOperationException(\"CommandText must be set before executing a command.\");\n        ValidateTransaction();","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/tursodatabase/turso/blob/c1e59287258d99b309e362a63f48822256e2f65f/bindings/dotnet/src/Turso.Data/TursoCommand.cs#L294-L330","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ncmd.CommandText = config.OptionalSql; // null in this env\nawait cmd.ExecuteNonQueryAsync(); // remote path -> throws\n\n// after\ncmd.CommandText = config.OptionalSql;\nif (!string.IsNullOrWhiteSpace(cmd.CommandText))\n    await cmd.ExecuteNonQueryAsync();","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(cmd.CommandText))\n    throw new InvalidOperationException(\"CommandText is empty; refusing to call the remote endpoint.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set CommandText via the constructor for remote commands.","Validate SQL loaded from configuration at startup.","Skip the execute call when optional SQL resolves empty."],"tags":["dotnet","ado-net","tursodb","sql","remote","validation"],"backgroundTag":"command-text-not-set","analyzedSha":"c1e59287258d99b309e362a63f48822256e2f65f","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}