{"record":{"id":"27239c17405f5348","repo":"tursodatabase/turso","slug":"cannot-convert-remote-type-value-to-double","errorCode":null,"errorMessage":"Cannot convert remote {Type} value to Double.","messagePattern":"Cannot convert remote (.+?) value to Double\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoRemoteClient.cs","lineNumber":687,"sourceCode":"    public long GetInt64()\n    {\n        return Type switch\n        {\n            \"integer\" => ParseInteger(),\n            \"float\" => checked((long)ParseFloat()),\n            \"text\" => long.Parse(Value.GetString() ?? \"\", CultureInfo.InvariantCulture),\n            _ => throw new InvalidCastException($\"Cannot convert remote {Type} value to Int64.\"),\n        };\n    }\n\n    public double GetDouble()\n    {\n        return Type switch\n        {\n            \"float\" => ParseFloat(),\n            \"integer\" => ParseInteger(),\n            \"text\" => double.Parse(Value.GetString() ?? \"\", CultureInfo.InvariantCulture),\n            _ => throw new InvalidCastException($\"Cannot convert remote {Type} value to Double.\"),\n        };\n    }\n\n    public decimal GetDecimal()\n    {\n        return Type switch\n        {\n            \"float\" => Convert.ToDecimal(ParseFloat(), CultureInfo.InvariantCulture),\n            \"integer\" => ParseInteger(),\n            \"text\" => decimal.Parse(Value.GetString() ?? \"\", CultureInfo.InvariantCulture),\n            _ => throw new InvalidCastException($\"Cannot convert remote {Type} value to Decimal.\"),\n        };\n    }\n\n    private long ParseInteger()\n    {\n        return Value.ValueKind == JsonValueKind.String\n            ? long.Parse(Value.GetString() ?? \"\", CultureInfo.InvariantCulture)","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data/TursoRemoteClient.cs#L669-L705","documentation":"GetDouble on a reader column whose remote value type is not float, integer, or text. NULL and BLOB values have no conversion arm, so a NULL cell read with GetDouble throws InvalidCastException rather than yielding a default.","triggerScenarios":"reader.GetDouble(i) on a remote connection where column i is NULL, a BLOB, or an unsupported type; numeric-looking text goes through double.Parse, which throws FormatException for non-numeric strings instead.","commonSituations":"Aggregates returning NULL on empty sets (AVG over zero rows); nullable REAL columns read without null checks; schema drift turning a REAL column into TEXT or BLOB.","solutions":["Guard with reader.IsDBNull(i) before GetDouble and substitute 0.0 or double.NaN as appropriate.","Use COALESCE/IFNULL in the SQL so the server never returns NULL for that column.","Verify the column ordinal and underlying type before switching to typed getters.","For text columns that should be numeric, parse manually with double.TryParse to control the failure mode."],"exampleFix":"// before\ndouble avg = reader.GetDouble(0);\n\n// after\ndouble avg = reader.IsDBNull(0) ? 0.0 : reader.GetDouble(0);","handlingStrategy":"validation","validationCode":"if (reader.IsDBNull(ordinal))\n    return 0.0; // or double.NaN as your domain sentinel\nreturn reader.GetDouble(ordinal);","typeGuard":"static bool CanReadDouble(System.Data.Common.DbDataReader reader, int i) =>\n    !reader.IsDBNull(i)\n    && reader.GetFieldType(i) is Type t\n    && (t == typeof(double) || t == typeof(long) || t == typeof(string));","tryCatchPattern":"try { value = reader.GetDouble(i); }\ncatch (InvalidCastException) when (reader.IsDBNull(i))\n{\n    value = 0.0;\n}","preventionTips":["Guard aggregates like AVG/MAX that return NULL on empty sets with COALESCE or IsDBNull checks.","Verify the column type after any schema migration before keeping typed getters.","Parse numeric-looking text columns with TryParse to control failures instead of letting casts throw.","Read by column name, not ordinal, to survive column reordering."],"tags":["turso","dotnet","reader","invalid-cast","null-handling"],"backgroundTag":"invalid-cast-exception","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}