{"record":{"id":"5e756ec046260a8f","repo":"DapperLib/Dapper","slug":"key","errorCode":null,"errorMessage":"key","messagePattern":"key","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Dapper/SqlMapper.DapperRow.cs","lineNumber":173,"sourceCode":"                if (index < 0 || index >= values.Length || values[index] is DeadValue) return false;\r\n                values[index] = DeadValue.Default;\r\n                return true;\r\n            }\r\n\r\n            object? IDictionary<string, object?>.this[string key]\r\n            {\r\n                get { TryGetValue(key, out object? val); return val; }\r\n                set { SetValue(key, value, false); }\r\n            }\r\n\r\n            public object? SetValue(string key, object? value)\r\n            {\r\n                return SetValue(key, value, false);\r\n            }\r\n\r\n            private object? SetValue(string key, object? value, bool isAdd)\r\n            {\r\n                if (key is null) throw new ArgumentNullException(nameof(key));\r\n                int index = table.IndexOfName(key);\r\n                if (index < 0)\r\n                {\r\n                    index = table.AddField(key);\r\n                }\r\n                else if (isAdd && index < values.Length && !(values[index] is DeadValue))\r\n                {\r\n                    // then semantically, this value already exists\r\n                    throw new ArgumentException(\"An item with the same key has already been added\", nameof(key));\r\n                }\r\n                return SetValue(index, value);\r\n            }\r\n            internal object? SetValue(int index, object? value)\r\n            {\r\n                int oldLength = values.Length;\r\n                if (oldLength <= index)\r\n                {\r\n                    // we'll assume they're doing lots of things, and\r","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/DapperLib/Dapper/blob/72a54c475f75e18cb93cba0809d00a5e6e49efd9/Dapper/SqlMapper.DapperRow.cs#L155-L191","documentation":"DapperRow.SetValue throws ArgumentNullException(nameof(key)) when the key is null. DapperRow is a dictionary-like row keyed by column name (resolved via the row's DapperTable), and a null key cannot be indexed. The same method also throws ArgumentException (\"An item with the same key has already been added\") on duplicate add, but the null-key guard fires first for null inputs.","triggerScenarios":"Calling row.SetValue(null, value) directly; a dynamic insert/update path that produced a null column name; passing a dictionary with a null key into a Dapper execute that materializes a DapperRow parameter.","commonSituations":"Building parameter dictionaries dynamically where a key ended up null (e.g. a column name lookup returned null); deserializing JSON into a dictionary that allowed a null key then passing it to Dapper.","solutions":["Null-check the key before calling SetValue / before adding to the parameter dictionary.","Filter out null keys when building dictionaries from dynamic/untrusted sources."],"exampleFix":"// before\nrow.SetValue(colName, val); // colName may be null\n\n// after\nif (colName is not null) row.SetValue(colName, val);","handlingStrategy":"validation","validationCode":"if (key is null) throw new ArgumentNullException(nameof(key));\nrow.SetValue(key, value);\n// or when building a dictionary: filter null keys first","typeGuard":"static bool IsValidKey(string? key) => key is not null;","tryCatchPattern":"try { row.SetValue(key, value); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"key\")\n{ /* skip or default the null key */ }","preventionTips":["Filter null keys out of dynamic dictionaries before passing them to Dapper.","Null-check column names derived from lookups/config before SetValue."],"tags":["argument-null","dapper-row","dictionary","dynamic"],"backgroundTag":null,"analyzedSha":"72a54c475f75e18cb93cba0809d00a5e6e49efd9","analyzedAt":"2026-08-13T14:18:18.115Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}