{"record":{"id":"2a2c820693fba2f8","repo":"tursodatabase/turso","slug":"missing-value-for-parameter-at-position-i","errorCode":null,"errorMessage":"Missing value for parameter at position {i}.","messagePattern":"Missing value for parameter at position (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoCommand.cs","lineNumber":208,"sourceCode":"                else\n                {\n                    var parameterIndex = i + 1;\n                    if (parameterIndex > parameterCount)\n                        throw new InvalidOperationException($\"Parameter at position {parameterIndex} was not found in the SQL statement.\");\n\n                    TursoBindings.BindParameter(preparedStatement, parameterIndex, parameter.ToValue());\n                    boundParameters[parameterIndex] = true;\n                }\n            }\n\n            for (var i = 1; i <= parameterCount; i++)\n            {\n                if (!boundParameters[i])\n                {\n                    var parameterName = TursoBindings.GetParameterName(preparedStatement, i);\n                    throw new InvalidOperationException(\n                        parameterName is null\n                            ? $\"Missing value for parameter at position {i}.\"\n                            : $\"Missing value for parameter {parameterName}.\");\n                }\n            }\n\n            _statement?.Dispose();\n            _statement = preparedStatement;\n            preparedStatement = null;\n        }\n        finally\n        {\n            preparedStatement?.Dispose();\n        }\n    }\n\n    protected override DbParameter CreateDbParameter()\n    {\n        return new TursoParameter();\n    }","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/tursodatabase/turso/blob/c1e59287258d99b309e362a63f48822256e2f65f/bindings/dotnet/src/Turso.Data/TursoCommand.cs#L190-L226","documentation":"After the binding loop, Prepare() verifies that every placeholder from 1..parameterCount received a value (TursoCommand.cs:195-205). An unbound positional placeholder (written as ?) has no retrievable name, so GetParameterName returns null and the error reports the 1-based position: 'Missing value for parameter at position {i}.'.","triggerScenarios":"SQL \"SELECT * FROM t WHERE a = ? AND b = ?\" with only one nameless parameter supplied; a conditional code path that skips one of several AddWithValue calls.","commonSituations":"One forgotten parameter among many ? placeholders; an AddWithValue inside an if-branch that did not run; SQL gained a WHERE clause but the parameter list was not updated.","solutions":["Supply exactly one positional parameter per ? placeholder, in statement order.","Validate placeholder count against Parameters.Count before executing (count '?' characters outside string literals).","Switch to named placeholders (@id) so any missing value is reported by name instead of position."],"exampleFix":"// before\ncmd.CommandText = \"SELECT * FROM t WHERE a = ? AND b = ?\";\ncmd.Parameters.AddWithValue(null, a); // b never supplied -> throws at position 2\n\n// after\ncmd.CommandText = \"SELECT * FROM t WHERE a = ? AND b = ?\";\ncmd.Parameters.AddWithValue(null, a);\ncmd.Parameters.AddWithValue(null, b);","handlingStrategy":"validation","validationCode":"var placeholders = CountPlaceholders(cmd.CommandText); // count '?' outside string literals\nvar supplied = cmd.Parameters.Cast<TursoParameter>().Count(p => string.IsNullOrEmpty(p.ParameterName));\nif (supplied < placeholders)\n    throw new InvalidOperationException($\"SQL needs {placeholders} positional values, got {supplied}.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    cmd.Prepare();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Missing value for parameter at position\"))\n{\n    // Parse the position from the message and add the missing value at that index.\n}","preventionTips":["Pair each ? with its value in a single helper that appends both together.","Prefer named placeholders so missing values are reported by name.","Add a debug assertion comparing placeholder count to Parameters.Count."],"tags":["dotnet","ado-net","tursodb","parameters","positional-parameters"],"backgroundTag":"missing-sql-parameter","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"}