{"record":{"id":"618342b00941ec34","repo":"DapperLib/Dapper","slug":"member-name-is-an-nameof-icustomqueryparamete","errorCode":null,"errorMessage":"Member '{name}' is an {nameof(ICustomQueryParameter)} and cannot be null","messagePattern":"Member '(.+?)' is an (.+?) and cannot be null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Dapper/SqlMapper.cs","lineNumber":3945,"sourceCode":"        private static MethodInfo? ResolveOperator(MethodInfo[] methods, Type from, Type to, string name)\r\n        {\r\n            for (int i = 0; i < methods.Length; i++)\r\n            {\r\n                if (methods[i].Name != name || methods[i].ReturnType != to) continue;\r\n                var args = methods[i].GetParameters();\r\n                if (args.Length != 1 || args[0].ParameterType != from) continue;\r\n                return methods[i];\r\n            }\r\n            return null;\r\n        }\r\n\r\n        /// <summary>\r\n        /// For internal use only\r\n        /// </summary>\r\n        [Obsolete(ObsoleteInternalUsageOnly, false)]\r\n        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]\r\n        public static void ThrowNullCustomQueryParameter(string name)\r\n            => throw new InvalidOperationException($\"Member '{name}' is an {nameof(ICustomQueryParameter)} and cannot be null\");\r\n\r\n        /// <summary>\r\n        /// Throws a data exception, only used internally\r\n        /// </summary>\r\n        /// <param name=\"ex\">The exception to throw.</param>\r\n        /// <param name=\"index\">The index the exception occurred at.</param>\r\n        /// <param name=\"reader\">The reader the exception occurred in.</param>\r\n        /// <param name=\"value\">The value that caused the exception.</param>\r\n        [Obsolete(ObsoleteInternalUsageOnly, false)]\r\n        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]\r\n        public static void ThrowDataException(Exception ex, int index, IDataReader reader, object? value)\r\n        {\r\n            Exception toThrow;\r\n            try\r\n            {\r\n                string name = \"(n/a)\", formattedValue = \"(n/a)\";\r\n                if (reader is not null && index >= 0 && index < reader.FieldCount)\r\n                {\r","sourceCodeStart":3927,"sourceCodeEnd":3963,"githubUrl":"https://github.com/DapperLib/Dapper/blob/72a54c475f75e18cb93cba0809d00a5e6e49efd9/Dapper/SqlMapper.cs#L3927-L3963","documentation":"Thrown (via ThrowNullCustomQueryParameter, SqlMapper.cs:3945) from generated parameter-setting IL when a property/field that implements ICustomQueryParameter (e.g. Dapper's DbString) is null on the parameter object. Dapper calls `ICustomQueryParameter.AddParameter` on the member; a null instance cannot have AddParameter called, so the emitted null-check calls ThrowNullCustomQueryParameter with the member name.","triggerScenarios":"A parameter object with a property of type DbString (or any ICustomQueryParameter) left null: `new Foo { Name = null }` where `public DbString Name`. The generated IL dup-checks the value and, if null, throws naming the member.","commonSituations":"Conditionally building a filter where a DbString property is only sometimes set and left null otherwise; reusing a DTO whose DbString member was never initialized; assuming DbString defaults like a normal string.","solutions":["Initialize the DbString/ICustomQueryParameter member to a non-null value (e.g. `new DbString { Value = name, IsAnsi = true, Length = 100 }`).","Make the member nullable and omit it from the parameter set when not needed (use a conditional parameter object or DynamicParameters).","Use a plain `string` property instead of DbString when you do not need ANSI/length control.","Build the parameter object so that DbString members are always populated before the query."],"exampleFix":"// before\nnew { Name = (DbString)null }\n// after\nnew { Name = new DbString { Value = name, IsAnsi = true, Length = 50 } }","handlingStrategy":"validation","validationCode":"// Ensure DbString/ICustomQueryParameter members are non-null before the query.\nif (p.Name is null) p.Name = new DbString { Value = name ?? (object)DBNull.Value, IsAnsi = true, Length = 50 };","typeGuard":"static bool AllCustomParamsSet(object p) => p.GetType().GetProperties().Where(pr => typeof(ICustomQueryParameter).IsAssignableFrom(pr.PropertyType)).All(pr => pr.GetValue(p) is not null);","tryCatchPattern":null,"preventionTips":["Always initialize DbString members to a non-null instance.","Use plain string properties when ANSI/length control is unnecessary.","Build parameter objects so DbString fields are populated in one place."],"tags":["dapper","dbstring","icustomqueryparameter","null","parameters"],"backgroundTag":null,"analyzedSha":"72a54c475f75e18cb93cba0809d00a5e6e49efd9","analyzedAt":"2026-08-13T14:18:18.115Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}