Inserting NULL values into Database tables using CSLA objects

Inserting NULL values into Database tables using CSLA objects

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


jvnbkk posted on Wednesday, November 21, 2007

Hi All,

Please help me out, how to insert NULL values into database tables using CSLA objects.  I am getting exception whenever I try to insert NULL values into a column using CSLA objects.  Is there any property needs to be created for this.

Thanks in Advance

Kiran


DavidDilworth replied on Wednesday, November 21, 2007

This is a really obvious question.  The column you are trying to insert the data into does allow NULL does it?

So you can write an INSERT statement that succesfully inserts a NULL value.

jvnbkk replied on Wednesday, November 21, 2007

Hi David,

My table allow NULL values to be inserted, still I am not able to insert NULL values using VB.NET code is there any clue.

Thanks

Kiran

richardb replied on Wednesday, November 21, 2007

The SmartDate CSLA datatype is an example how a .Net date type has been customised to allow NULLS in dates.

If you are working with other SQL data types, ints, etc you may find that the .Net types won't let you store a null.  For example, an integer will probably get defaulted to zero - remember the internal variables in your class that are often initialised to help make binding work.

This can be a problem when you come to insert the data.  It's zero but you want a null to be saved.

Well maybe you could create a SmartInt data type.  I've seen other solutions where just before you insert the data (either in the DataPortal or Stored procedure), the code checks for a certain value and changes it to a null.

I thought I read something once about the .Net data types being changed to allow Nulls but perhaps I just imagined that.

There's probably some further advice on this site if you do a search.

DavidDilworth replied on Wednesday, November 21, 2007

What type is the field that you are trying to persist?  Are you using SPs? Perhaps you can post some code snippets to help?

@Richard:  .NET 2.0 supports the concept of nullable types.  I think that is what you meant.  Check out this link for a brief description.

richardb replied on Thursday, November 22, 2007

Ah yes, Nullable Types - thanks thats it.  I knew I read it somewhere.

Just come out of a 1.52 year massive CSLA1.x project and only now am moving to a .Net 2.0 and upwards world.

Thanks.

ward0093 replied on Wednesday, November 21, 2007

I use the new Nullable Types almost everywhere in my code and BOs because my DB has a lot of fields that are suppose to be NULL when no data has been presented/persisted.  For examble: Nullable(of int32) or Nullable(of Uint16).

There is some important discoveries when persisting Nullable Data to the database via a Stored Proc...  First, in your stored proc., make sure that all nullable parameters have default values (i.e. @customerAge as smallint = null)

This is important because if you pass the parameter in like this:

sqlCmd.Parameters.AddWithValue("@customerAge", mCustomerAge) where mCustomerAge is a Nullable(of UInt16); the SP will default to the default value in the SP... if there is no default value in the SP parameter and your BO/mCustomerAge is Null/Nothing... the sqlCmd will throw an exception saying that "@customerAge parameter is required and not provided" (or something like that).

Also notice that you DO NOT have to perform a check on the BO mCustomerAge "Value" or "HasValue" or "GetDefaultValue"... the SP and the parameter Default Value takes care of it for you...

So this should be how you persist your Nullable data to the database.

TO FETCH:

I had to override and create a custom SafeDataReader from Rockys SafeDataReader.... to load nullable types into my BO's... see post http://forums.lhotka.net/forums/thread/19060.aspx

this show the function GetValue(of T) that i created to handle loading of Nullable Types in BOs

 

Hope this helps

ward0093

jvnbkk replied on Sunday, November 25, 2007

Hi ward,

Thanks I will try using Nullable data

Thanks again


Copyright (c) Marimer LLC