If blank string enter null into database...

Last post 05-09-2007, 9:11 PM by Damon. 1 replies.
Sort Posts: Previous Next
  •  05-09-2007, 10:37 AM Post number 25629

    • Adam.Smith is not online. Last active: 18 Aug 2007, 3:58 AM Adam.Smith
    • Top 50 Contributor
    • Joined on 07-19-2006
    • Barton Le Clay, Bedfordshire
    • Level 2: Deep Blue

    If blank string enter null into database...

    Hi there,

    I'm having some trouble! I am writing strings to a database varchar field that allows nulls but when I try to pass a string with the value of dbnull from a Data Row Field into the Data Sets update method it throws an invalid cast exception. I understand why but it just does not make sense to me in this context... The db field allows null so therefore the update method should allow it too. I understand String are not allowed to be nullable types. But something is missing here...

    The DAL is a strongly typed dataset created using strong typed DataTable objects

    Can anyone explain this to me (this also happens with numeric values) and/or suggest a solution for this.


    Thanks for reading!

    (A highly panicked Adam)

  •  05-09-2007, 9:11 PM Post number 25707 in reply to post number 25629

    • Damon is not online. Last active: 11-26-2008, 11:06 AM Damon
    • Top 10 Contributor
    • Joined on 06-26-2006
    • Dallas, TX
    • Acorn Archimedes

    Re: If blank string enter null into database...

    Ah, the wonders of null.  Try this assignment

    DataField = string.IsNullOrEmpty(YourStringValue) ? DBNull.value : YourStringValue

    ADO.NET doesn't understand null, but it does understand DBNull.


    Damon Armstrong, Technology Consultant
    [Blog] [Articles]
View as RSS news feed in XML