Skip to content

System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.DBNull' #6

@FoggyFinder

Description

@FoggyFinder

Call of dr.IsDBNull throws InvalidCastException.

MCVE
Replace printUser function from the Sample with one below:

let printUser (dr: IDataRecord) =
    let id = (dr?id).Value
    let name = (dr?name).Value
    let address = 
        let ai = dr.GetOrdinal("address")
        if dr.IsDBNull ai then None
        else dr.GetString(ai) |> Some
        |> function
        | None -> "No registered address"
        | Some x -> x
    printfn "Id: %d; Name: %s; Address: %s" id name address
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.DBNull'.
   at Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.UnboxGeneric[T](Object source) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\prim-types.fs:line 598
   at FsSqlImpl.DictDataRecord.System-Data-IDataRecord-IsDBNull(Int32 i)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions