{"record":{"id":"09761ee0c4b7351e","repo":"tursodatabase/turso","slug":"cannot-convert-sourcetype-to-targettype","errorCode":null,"errorMessage":"Cannot convert {sourceType} to {targetType}.","messagePattern":"Cannot convert (.+?) to (.+?)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnectionStringBuilder.cs","lineNumber":347,"sourceCode":"            \"DateTimeKind\" => System.DateTimeKind.Unspecified,\n            \"DateTimeFormat\" => string.Empty,\n            \"BinaryGUID\" => true,\n            \"Version\" => 3,\n            _ => throw new ArgumentException(Properties.Resources.KeywordNotSupported(keyword)),\n        };\n    }\n\n    private static TEnum ConvertEnum<TEnum>(object value)\n        where TEnum : struct\n    {\n        if (value is TEnum typedValue)\n            return typedValue;\n\n        if (value is string stringValue)\n            return Enum.Parse<TEnum>(stringValue, ignoreCase: true);\n\n        if (value.GetType().IsEnum && value is not TEnum)\n            throw new ArgumentException(Properties.Resources.ConvertFailed(value.GetType(), typeof(TEnum)));\n\n        var enumValue = (TEnum)Enum.ToObject(typeof(TEnum), value);\n        if (!Enum.IsDefined(typeof(TEnum), enumValue))\n            throw new ArgumentOutOfRangeException(nameof(value), value, Properties.Resources.InvalidEnumValue(typeof(TEnum), enumValue));\n\n        return enumValue;\n    }\n\n    private static bool? ConvertToNullableBoolean(object value)\n        => value is null or string { Length: 0 }\n            ? null\n            : Convert.ToBoolean(value, CultureInfo.InvariantCulture);\n\n    private static SqliteOpenMode ConvertOpenMode(object value)\n    {\n        var mode = ConvertEnum<SqliteOpenMode>(value);\n        if (!Enum.IsDefined(mode))\n            throw new ArgumentOutOfRangeException(nameof(value), value, Properties.Resources.InvalidEnumValue(typeof(SqliteOpenMode), mode));","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnectionStringBuilder.cs#L329-L365","documentation":"ArgumentException thrown by ConvertEnum when the value being stored is a boxed enum of a different enum type than the target option — for example assigning a DateTimeKind to the 'Mode' keyword or a SqliteCacheMode to 'DateTimeKind'. The binding refuses cross-enum conversion because the underlying numeric meanings differ. Strings and non-enum values follow other paths, so this fires specifically for mismatched enum-typed objects.","triggerScenarios":"builder[\"Mode\"] = DateTimeKind.Utc; builder[\"DateTimeKind\"] = SqliteCacheMode.Shared; passing a boxed foreign enum from generic/config code like builder[keyword] = Enum.Parse(otherEnumType, text).","commonSituations":"Generic configuration loaders that store parsed enum objects by keyword name; copy-paste between option lines swapping the values; dictionary-driven setups mapping config sections to keywords by reflection.","solutions":["Assign the correct enum type to each keyword: SqliteOpenMode for Mode, SqliteCacheMode for Cache, DateTimeKind for DateTimeKind.","Prefer the typed properties (builder.Mode = ..., builder.DateTimeKind = ...) which make the mistake a compile error.","In generic loaders, convert to the expected type via Enum.Parse(targetType, value.ToString()) or store the string form, which the binder parses by name."],"exampleFix":"// before\nbuilder[\"DateTimeKind\"] = SqliteCacheMode.Shared; // wrong enum type\n\n// after\nbuilder[\"DateTimeKind\"] = DateTimeKind.Utc;","handlingStrategy":"type-guard","validationCode":"static object ConvertForKeyword(string keyword, object value) => keyword switch\n{\n    \"Mode\" when value is string or SqliteOpenMode => value,\n    \"Cache\" when value is string or SqliteCacheMode => value,\n    \"DateTimeKind\" when value is string or DateTimeKind => value,\n    _ => throw new ConfigException($\"Value type {value?.GetType().Name} wrong for {keyword}\"),\n};","typeGuard":"static bool MatchesKeywordType(string keyword, object value) => keyword switch\n{\n    \"Mode\" => value is string or SqliteOpenMode,\n    \"Cache\" => value is string or SqliteCacheMode,\n    \"DateTimeKind\" => value is string or DateTimeKind,\n    _ => value is string,\n};","tryCatchPattern":"try { builder[kw] = value; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Cannot convert\"))\n{ /* convert value to string first: builder[kw] = value.ToString() */ }","preventionTips":["Store enum options as their string names when values come from generic config.","Prefer typed properties so the compiler enforces the enum type."],"tags":["csharp","dotnet","connection-string","enum","type-conversion"],"backgroundTag":"type-conversion-failed","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}