Questions about SafeDataReader, Null columns

Questions about SafeDataReader, Null columns

Old forum URL: forums.lhotka.net/forums/t/8750.aspx


Jav posted on Thursday, April 01, 2010

I have many bit columns which (rightfully) can be true, false or null.  When I read them into Properties declared as bool? using the SafeDataReader, I find that the values of each Property are faithfully set as true, false or null - and I am happy because my Three-State Checkboxes work just fine.

But then I am listening to the SL Video on DataAccess and Rocky says, "if you are using null columns, then do not use SafeDataReader".   "Woh", I say, is that a fluke that everything is working fine?"  So, I decided that I better ask.  I think, SafeDataReader ought to work as it is working right now since SqlServer allows three states for a bit column, a CheckBox allows for three states, SafeDataReader allows for true, false or null values for a bool? property, and life is good.

Jav

ajj3085 replied on Thursday, April 01, 2010

Well, the whole point of SafeDataReader is that it frees you from worrying about null values, which you usually don't care about.

If you don't want that behavior for bool or any other datatype, don't use the SafeDataReader.

That said, something is up, because looking at the code GetBoolean should in fact be returning false when it encounters a null from the database.  So I suggest you double check your code, but you shouldn't be seeing what you're seeing.

Jav replied on Thursday, April 01, 2010

Hi Andy,

I do not use GetBoolean - that indeed was only returning true or false.  I use GetValue() and that returns true, false or null.  I expect that that is by design.

Jav

Jav replied on Thursday, April 01, 2010

Also, I would hate not to be able to use SafeDataReader.  Just to be able to use Column names, instead of [0], [1] etc, is worth every penny.

Jav

Jav replied on Thursday, April 01, 2010

Okay, I have looked it up now - should have done that in the first place.  GetValue does return null if DataReader returns null, whereas GetBoolean changes nulls to false. 

Then perhaps we should say that we can still use SafeDataReader but use GetValue() instead of the more specific GetWhatever() for those columns where we do want to use nulls.  I have at least one, often more than one, SmartDate field in every Table - so switching to SqlDataReader wouldn't work for me.  Have never used it - that's why I didn't know that GetOrdinal is a method in IDataReader, not an add on in SafeDataReader. Learning!

Jav

pinchers1jm replied on Thursday, April 01, 2010

I subclassed the the SafeDataReader class to create the attached NullableDataReader class which is able to handle both nullable and non nullable columns.

 

Jav replied on Thursday, April 01, 2010

pinchers1jm

I subclassed the the SafeDataReader class to create the attached NullableDataReader class which is able to handle both nullable and non nullable columns.


But you don't need to do all that work.  For any DataColumn where you need to have your nullable column's null value placed in your Property, simply use:

dr.GetValue("MyNullableColumnName"), instead of

dr.GetString("MyNullableColumnName") or dr.GetWhatever("").

The GetValue() code in Csla first checks to see if the DataReader has returned a null.  If so it will hand that null to you.

Jav

 

Copyright (c) Marimer LLC