{"record":{"id":"cdeaf284214036fd","repo":"tursodatabase/turso","slug":"parameter-at-position-parameterindex-was-not-fou","errorCode":null,"errorMessage":"Parameter at position {parameterIndex} was not found in the SQL statement.","messagePattern":"Parameter at position (.+?) was not found in the SQL statement\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoCommand.cs","lineNumber":194,"sourceCode":"            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\n                if (!string.IsNullOrEmpty(parameter.ParameterName))\n                {\n                    var parameterIndex = TursoBindings.BindNamedParameter(preparedStatement, parameter.ParameterName, parameter.ToValue());\n                    if (parameterIndex == 0)\n                        throw new InvalidOperationException($\"Parameter {parameter.ParameterName} was not found in the SQL statement.\");\n\n                    boundParameters[parameterIndex] = true;\n                }\n                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","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/tursodatabase/turso/blob/c1e59287258d99b309e362a63f48822256e2f65f/bindings/dotnet/src/Turso.Data/TursoCommand.cs#L176-L212","documentation":"Positional (nameless) parameters are bound by collection order: the i-th parameter maps to placeholder index i+1 (TursoCommand.cs:184-188). If the collection holds more positional parameters than the statement has placeholders, i+1 exceeds GetParameterCount and Prepare throws InvalidOperationException reporting the offending position.","triggerScenarios":"SQL \"INSERT INTO t VALUES (?, ?)\" with three nameless parameters added to Parameters; a loop that appends positional parameters without matching the placeholder count.","commonSituations":"Parameters added in a loop over a list whose length drifted from the SQL; a reused command object carrying leftover positional parameters; dynamic SQL that dropped a placeholder during editing.","solutions":["Make the count of nameless parameters exactly equal the number of ? placeholders in the SQL.","Call cmd.Parameters.Clear() before re-running a command with different SQL or parameter sets.","Prefer named parameters when SQL is built dynamically so count/order drift cannot silently shift bindings."],"exampleFix":"// before\ncmd.CommandText = \"INSERT INTO t VALUES (?, ?)\"; // 2 placeholders\ncmd.Parameters.AddWithValue(null, a);\ncmd.Parameters.AddWithValue(null, b);\ncmd.Parameters.AddWithValue(null, c); // 3rd positional -> throws at position 3\n\n// after\ncmd.CommandText = \"INSERT INTO t VALUES (?, ?)\";\ncmd.Parameters.AddWithValue(null, a);\ncmd.Parameters.AddWithValue(null, b);","handlingStrategy":"validation","validationCode":"var positional = cmd.Parameters.Cast<TursoParameter>().Count(p => string.IsNullOrEmpty(p.ParameterName));\nvar placeholders = CountPlaceholders(cmd.CommandText); // count '?' outside string literals\nif (positional > placeholders)\n    throw new InvalidOperationException($\"{positional} positional parameters but only {placeholders} '?' placeholders.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add exactly one nameless parameter per ? placeholder, in order.","Call cmd.Parameters.Clear() before re-running a command with new SQL.","Prefer named parameters for dynamically built SQL."],"tags":["dotnet","ado-net","tursodb","parameters","positional-parameters"],"backgroundTag":"sql-parameter-not-found","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"}