{"record":{"id":"e5da67dbc22c68da","repo":"tursodatabase/turso","slug":"commandtext-must-be-set-before-preparing-a-command","errorCode":null,"errorMessage":"CommandText must be set before preparing a command.","messagePattern":"CommandText must be set before preparing a command\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoCommand.cs","lineNumber":163,"sourceCode":"    {\n        await using var reader = await ExecuteDbDataReaderAsync(CommandBehavior.Default, cancellationToken).ConfigureAwait(false);\n        return await reader.ReadAsync(cancellationToken).ConfigureAwait(false)\n            ? reader.GetValue(0)\n            : null;\n    }\n\n    public override void Prepare()\n    {\n        using var syncOperation = _connection?.EnterSyncOperation();\n        PrepareCore();\n    }\n\n    private void PrepareCore()\n    {\n        if (_connection is null)\n            throw new InvalidOperationException(\"Connection must be set before preparing a command.\");\n        if (string.IsNullOrWhiteSpace(CommandText))\n            throw new InvalidOperationException(\"CommandText must be set before preparing a command.\");\n        ValidateTransaction();\n        if (_connection.IsRemote)\n            return;\n\n        TursoStatementHandle? preparedStatement = null;\n        try\n        {\n            var sql = RewriteFacadePragmas(CommandText, _connection);\n            preparedStatement = TursoBindings.PrepareStatement(_connection.Turso, sql);\n            var parameterCount = TursoBindings.GetParameterCount(preparedStatement);\n            var boundParameters = new bool[parameterCount + 1];\n\n            for (var i = 0; i < _parameterCollection.Count; i++)\n            {\n                var parameter = _parameterCollection[i] as TursoParameter;\n                if (parameter == null)\n                    throw new ArgumentException(\"Parameter must be of type TursoParameter\");\n","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/tursodatabase/turso/blob/c1e59287258d99b309e362a63f48822256e2f65f/bindings/dotnet/src/Turso.Data/TursoCommand.cs#L145-L181","documentation":"Prepare() rejects a null, empty, or whitespace-only CommandText with InvalidOperationException (TursoCommand.cs:156-157) because there is no SQL to compile into a native statement. The check uses IsNullOrWhiteSpace, so blank strings are treated like missing SQL.","triggerScenarios":"cmd.Prepare() (or the first local Execute, which invokes Prepare) when CommandText was never set, set to \"\", or contains only spaces/tabs/newlines.","commonSituations":"SQL assembled from a template or configuration that resolves to empty in some environment; a conditional branch that was supposed to set CommandText but was skipped; copy-paste where the assignment line was lost.","solutions":["Set the SQL before Prepare/Execute: cmd.CommandText = sql; or use new TursoCommand(sql, conn).","If SQL is built dynamically, guard the empty case and skip the database round-trip entirely.","Assert !string.IsNullOrWhiteSpace(cmd.CommandText) in debug builds or startup validation to catch config drift early."],"exampleFix":"// before\nvar cmd = conn.CreateCommand();\ncmd.Prepare(); // CommandText never set -> throws\n\n// after\nvar cmd = conn.CreateCommand();\ncmd.CommandText = \"SELECT * FROM users WHERE id = @id\";\ncmd.Prepare();","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(cmd.CommandText))\n    throw new InvalidOperationException(\"Set CommandText before preparing the command.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set CommandText from the constructor: new TursoCommand(sql, conn).","Validate required SQL strings at startup when they come from configuration.","Skip Prepare when the built SQL turns out empty instead of calling through."],"tags":["dotnet","ado-net","tursodb","sql","prepare"],"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-14T05:17:10.506Z"}