get property of datatable throws exception on NULL value. Why?

Last post 05-20-2008 6:16 AM by Abhishek khanna. 5 replies.

Sort Posts:

  • get property of datatable throws exception on NULL value. Why?

    05-16-2008, 5:33 AM
    • Loading...
    • ElCapitan
    • Joined on 12-08-2004, 7:53 AM
    • Posts 88

     Hi all,

    When I get a the property of a row of a strongly typed dataset, the designer writes code so it throws an exception if the value of the property is NULL because it can't convert NULL value to an integer. Is there anyway to allow nullable type for this property because it seems I can only set the default value of the property to a number which is not ideal especially if the property is a foreign key and means something when it is null. Or, use the IsFieldNameNull function which means that I have to scatter this code everywhere which is not really a very clean solution either.

     I preferably would like to grab the property no matter what it is and deal with the value, if integer or null and deal with it myself.

     
    I hope there is a solution. Thanks so your help.
     


    Filed under: , ,
  • Re: get property of datatable throws exception on NULL value. Why?

    05-16-2008, 6:37 AM

    Hi

    Why dont you make this property type a nullable value type like below:

    int? x;

    A nullable value type can hold a null & a value type. So your integer can also contain a null & this wont throw an exception.

    You can google nullable value type for more information or ask me more.

    Hope this helps.

     

  • Re: get property of datatable throws exception on NULL value. Why?

    05-16-2008, 8:42 AM
    • Loading...
    • ElCapitan
    • Joined on 12-08-2004, 7:53 AM
    • Posts 88

     The property I am referring to is a column in a datatable in a strongly typed dataset. The designer in VS 2005 writes the code when editing the .xsd dataset. I know if I had my own business objects I could use the nullable type, however in the auto generated dataset I don't believe I can set it to a nullable type.

     I look forward to your comments. Thanks for your time.
     

  • Re: get property of datatable throws exception on NULL value. Why?

    05-19-2008, 10:52 AM
    Answer

    Hi

    You are right typed dataset dont seem to be supportive of having a field as nullable value type. But i was wondering about your problem & thinking why are we making this more complicated when the solution is simple. why dont you use sql server's ISNULL function to return an integer in your query.

    Ex: ISNULL(fieldName, 0))

    So it will return 0 if the value is null & you can handle this accordingly in your DAL layer.

    Hope this helps.

  • Re: get property of datatable throws exception on NULL value. Why?

    05-20-2008, 3:55 AM
    • Loading...
    • ElCapitan
    • Joined on 12-08-2004, 7:53 AM
    • Posts 88

    Its a good point. I was hoping to stay away from 'magic numbers'. I was hoping that the GetColumnValue function was overidable so I can do what I need with it.

    By putting it to zero using ISNULL, I will not know if the actual value was zero or null because the actual value in the database could be zero as well.

    If it can't be done directly then that is the answer I am looking for. I am sure 'they' will include nullable types in datasets next time around.

    Thanks for spending time with this post. 

  • Re: get property of datatable throws exception on NULL value. Why?

    05-20-2008, 6:16 AM

    Hi 

    Yes lets hope typed datasets do get advantages offered by nullable types.

    Also, if you feel my post answered you then please mark as answer.

    Thanks

    Abhishek

     

Page 1 of 1 (6 items)