{"record":{"id":"147282e19c3218a4","repo":"tursodatabase/turso","slug":"parameter-parameter-parametername-was-not-found","errorCode":null,"errorMessage":"Parameter {parameter.ParameterName} was not found in the SQL statement.","messagePattern":"Parameter (.+?) was not found in the SQL statement\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoCommand.cs","lineNumber":186,"sourceCode":"        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\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                {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/tursodatabase/turso/blob/c1e59287258d99b309e362a63f48822256e2f65f/bindings/dotnet/src/Turso.Data/TursoCommand.cs#L168-L204","documentation":"While binding named parameters, TursoBindings.BindNamedParameter returns the 1-based index of the matching SQL placeholder or 0 when the prepared statement contains no parameter with that name (TursoCommand.cs:176-180). A zero result makes Prepare throw InvalidOperationException naming the offending parameter — the collection and the SQL text disagree on a name.","triggerScenarios":"cmd.Parameters.AddWithValue(\"@usr\", 42) while the SQL uses @user (typo or prefix style mismatch); supplying named parameters to SQL written with positional ? placeholders; SQL edited after the parameter code was written.","commonSituations":"Typo or @/no-prefix inconsistency between the AddWithValue call and the SQL string; refactors that rename a column/parameter in SQL but not in code; mixed named/positional styles inside one statement.","solutions":["Match the ParameterName exactly to the placeholder in the SQL, including the @ prefix.","Keep the AddWithValue call physically next to the SQL it serves, or derive parameter names from the SQL text.","Pick one style per statement: all named (@x) placeholders with named parameters, or all ? with positional parameters."],"exampleFix":"// before\ncmd.CommandText = \"SELECT * FROM users WHERE id = @user\";\ncmd.Parameters.AddWithValue(\"@usr\", 42); // name mismatch -> throws\n\n// after\ncmd.CommandText = \"SELECT * FROM users WHERE id = @user\";\ncmd.Parameters.AddWithValue(\"@user\", 42);","handlingStrategy":"validation","validationCode":"var names = Regex.Matches(cmd.CommandText, @\"(?<![@\\w])@(\\w+)\").Select(m => m.Groups[1].Value).ToHashSet(StringComparer.OrdinalIgnoreCase);\nforeach (TursoParameter p in cmd.Parameters)\n{\n    if (!string.IsNullOrEmpty(p.ParameterName) && !names.Contains(p.ParameterName.TrimStart('@')))\n        throw new InvalidOperationException($\"{p.ParameterName} does not appear in the SQL text.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    cmd.Prepare();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"was not found in the SQL statement\"))\n{\n    // Read the parameter name from the message and align it with the SQL placeholder.\n}","preventionTips":["Keep AddWithValue calls adjacent to the SQL string they parameterize.","Use one naming style (all @name or all ?) per statement.","Re-check parameter names after any SQL edit."],"tags":["dotnet","ado-net","tursodb","parameters","sql","named-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-14T00:17:10.932Z"}