none
DataRow/SqlParameter and .NET 4.0: Ambiguity solved between DBNull.Value and Nullable Data Types? RRS feed

  • Question

  • Just out of curiosity:

    In .NET 2.0 it was quite awkward to test for database NULL values in DataRows or to use type safe nullable database columns in .NET at all.

    Is there a solution available in the framework meanwhile that can deal with the framework's nullable data types and renders it easier/safe to use them with DataRow values or SqlParameter values?

     


    Vote here for a Microsoft Connect feedback channel on Windows - and win a better Windows!

    • Edited by BetterToday Wednesday, February 1, 2012 1:00 PM
    Wednesday, February 1, 2012 1:00 PM

Answers

All replies

  • Hi BetterToday,

    Welcome!

    Based on my understanding, Nullable represent value-type variables that can be assigned the value of null, we can use .HasValue to test for null.

    SqlParameter Parameter = new SqlParameter("@Age", SqlDbType.Int);
    Parameter.Value = person.Age.HasValue ? person.Age.Value: DBNull.Value;
    
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 2, 2012 8:35 AM
    Moderator
  • Hi Allan,

    thank you for your reply.

    SqlParameter Parameter = new SqlParameter("@Age", SqlDbType.Int); Parameter.Value = person.Age.HasValue ? person.Age.Value: DBNull.Value;

    Yes, you are right. But this solution is quite a hassle.

    I wondered whether in the meantime (after discussions in this forum years ago) Microsoft has added nullable data type support to ADO.NET, so that the following became possible:

    int? a = null;

    a = DBNull.Value

    SqlParameter Parameter = new SqlParameter("@Age", SqlDbType.Int);
    Parameter.Value = a;
    Parameter.Direction = ParameterDirection.InputOutput;
    ...
    a = Parameter.Value;

    Best regards,
    Axel

     


    Vote here for a Microsoft Connect feedback channel on Windows - and win a better Windows!


    • Edited by BetterToday Thursday, February 2, 2012 10:03 AM
    Thursday, February 2, 2012 10:01 AM
  • Hi BetterTody,

    Thanks for your feedback.

    I'd like to help you to report your wishes, thanks for understanding.

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 3, 2012 7:10 AM
    Moderator
  • No, we have not added Nullable<t> support for ADO.NET parameters.  So you must either assign null or DbNull.Value to the parameter.
    Matt
    Friday, February 3, 2012 6:58 PM
    Moderator
  • Thanks Matt,

    is this about to come?

     

     


    Vote here for a Microsoft Connect feedback channel on Windows - and win a better Windows!

    Friday, February 3, 2012 7:05 PM
  • I would file a request via https://connect.microsoft.com/VisualStudio/Feedback to keep this feature on the radar for the ADO.NET team.


    Matt
    Friday, February 3, 2012 7:19 PM
    Moderator