Skip to content

How to deal with DBNull but want to go into a normal field #164

Open

Description

@Smurf-IV

So the conversion works ok with Dapper reflection, but with AOT it throws at this point
result.PrimaryIncome = GetValue<double>(reader, columnOffset);

I think the value might be null, but the field type is not "Nullable" (Legacy code, and is also serialised elsewhere, so defo cannot change!)

Is there some attribute that can be applied to state that DBNulls are converted to zero's (Or even Nan for doubles?)

Code:

            IEnumerable<MLModelInput> result = connection.Query<MLModelInput>(sProcName,
                    new { PoolCutOffDate = poolCutOffDate },
                    buffered: false,
                    commandTimeout: 1800,   // Set timeout to 30 mins
                    commandType: CommandType.StoredProcedure);
            return result.ToArray();  // <- Boom with AOT!!

Activity

  1. Smurf-IV commented on Jul 28, 2025

    @Smurf-IV
    Author

    The "Code" that used to be used before using dapper was this:

                                var value = double.NaN;
                                if (cellValue != DBNull.Value)
                                {
                                    value = Convert.ToDouble(cellValue);
                                }

    Being able to do something similar with AOT via an attributes or Query API call setting would be ideal

  2. Smurf-IV commented on Jul 28, 2025

    @Smurf-IV
    Author

    Not sure this is relevant, but I tried this when the exception was thrown:

    reader.GetDouble(columnOffset)
    'reader.GetDouble(columnOffset)' threw an exception of type 'System.InvalidOperationException'
        Data: {System.Collections.ListDictionaryInternal}
        HResult: -2146233079
        HelpLink: null
        InnerException: null
        Message: "Invalid attempt to read from column ordinal '22'.  With CommandBehavior.SequentialAccess, you may only read from column ordinal '199' or greater."
        Source: "Microsoft.Data.SqlClient"
        StackTrace: "   at Microsoft.Data.SqlClient.SqlDataReader.CheckDataIsReady(Int32 columnIndex, Boolean allowPartiallyReadColumn, Boolean permitAsync, String methodName)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.TryReadColumn(Int32 i, Boolean setTimeout, Boolean allowPartiallyReadColumn, Boolean forStreaming)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout, Boolean allowPartiallyReadColumn)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)\r\n   at <>x.<>m0(RowFactory0 <>4__this, DbDataReader reader, ReadOnlySpan`1 tokens, Int32 columnOffset, Object state)"
        TargetSite: {Void CheckDataIsReady(Int32, Boolean, Boolean, System.String)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions