{"record":{"id":"11df8af384f8e34c","repo":"DapperLib/Dapper","slug":"if-specifying-isfixedlength-a-length-must-also-b","errorCode":null,"errorMessage":"If specifying IsFixedLength,  a Length must also be specified","messagePattern":"If specifying IsFixedLength,  a Length must also be specified","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Dapper/DbString.cs","lineNumber":75,"sourceCode":"        public string? Value { get; set; }\r\n\r\n        /// <summary>\r\n        /// Gets a string representation of this DbString.\r\n        /// </summary>\r\n        public override string ToString() => Value is null\r\n            ? $\"Dapper.DbString (Value: null, Length: {Length}, IsAnsi: {IsAnsi}, IsFixedLength: {IsFixedLength})\"\r\n            : $\"Dapper.DbString (Value: '{Value}', Length: {Length}, IsAnsi: {IsAnsi}, IsFixedLength: {IsFixedLength})\";\r\n\r\n        /// <summary>\r\n        /// Add the parameter to the command... internal use only\r\n        /// </summary>\r\n        /// <param name=\"command\"></param>\r\n        /// <param name=\"name\"></param>\r\n        public void AddParameter(IDbCommand command, string name)\r\n        {\r\n            if (IsFixedLength && Length == -1)\r\n            {\r\n                throw new InvalidOperationException(\"If specifying IsFixedLength,  a Length must also be specified\");\r\n            }\r\n            bool add = !command.Parameters.Contains(name);\r\n            IDbDataParameter param;\r\n            if (add)\r\n            {\r\n                param = command.CreateParameter();\r\n                param.ParameterName = name;\r\n            }\r\n            else\r\n            {\r\n                param = (IDbDataParameter)command.Parameters[name];\r\n            }\r\n#pragma warning disable 0618\r\n            param.Value = SqlMapper.SanitizeParameterValue(Value);\r\n#pragma warning restore 0618\r\n            if (Length == -1 && Value is not null && Value.Length <= DefaultLength)\r\n            {\r\n                param.Size = DefaultLength;\r","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/DapperLib/Dapper/blob/72a54c475f75e18cb93cba0809d00a5e6e49efd9/Dapper/DbString.cs#L57-L93","documentation":"DbString.AddParameter throws InvalidOperationException when IsFixedLength is true but Length is still its default of -1. A fixed-length string (CHAR/NCHAR) requires an explicit size so the provider can size the parameter; without it the command would be malformed. The message (with its double space) is a known string in the Dapper source.","triggerScenarios":"Building a DbString, setting IsFixedLength=true, and never assigning Length (it defaults to -1); then passing that DbString as a parameter so AddParameter runs during command setup.","commonSituations":"Migrating dynamic SQL that used variable-length VARCHAR to fixed-length CHAR without updating parameter setup; copy-pasting a DbString config block and dropping the Length line; reading Length from config where the key was absent.","solutions":["Set an explicit Length matching the column size whenever IsFixedLength=true (e.g. Length = 50).","If the column is variable length, leave IsFixedLength=false (default) so Length=-1 is allowed.","Centralize DbString construction in a helper that asserts IsFixedLength implies a positive Length."],"exampleFix":"// before\nvar p = new DbString { Value = \"abc\", IsFixedLength = true };\ncnn.Execute(\"insert into T(C) values (@c)\", new { c = p });\n\n// after\nvar p = new DbString { Value = \"abc\", IsFixedLength = true, Length = 50 };","handlingStrategy":"validation","validationCode":"var p = new DbString { Value = s, IsFixedLength = true };\nif (p.IsFixedLength && p.Length <= 0) throw new InvalidOperationException(\"Fixed-length DbString requires a positive Length\");\ncnn.Execute(sql, new { c = p });","typeGuard":null,"tryCatchPattern":"try { cnn.Execute(sql, new { c = p }); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IsFixedLength\"))\n{ /* set Length and retry / log config error */ }","preventionTips":["Always set Length when IsFixedLength=true; treat the pair as inseparable.","Wrap DbString construction in a helper that asserts the Length invariant."],"tags":["dbstring","parameter-binding","configuration"],"backgroundTag":null,"analyzedSha":"72a54c475f75e18cb93cba0809d00a5e6e49efd9","analyzedAt":"2026-08-13T14:18:18.115Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}