0

Ok, So I am trying to set a datetime column to null via SubSonic 2.x

DateTime? dt = new DateTime();
dt= null;
Datum pd = (new DatumCollection()
                    .Where(Datum.Columns.Data, cp[0].Data)
                    .Load())[0];

                pd.dtAcceptance = (DateTime)dt;
                pd.Save();

Even though the database column allows for a null value, it won't save and I've tried a couple of other options, including setting it to DateTime.MinValue (1/1/0001 is too early a date I guess). I get 'Nullable object must have a value'

I've been searching here and Google for some magic bullet to fix what I think is a pretty stupid problem to have. Is there something in the SubSonic generation I need to change to allow it to work or do I have to write some stupid trigger on the SQL side that if a date comes in with '1/1/1900' to change it to NULL ?

help and thanks!

stack: "Nullable object must have a value." at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable`1.get_Value() at Services.Decision.OnDecline(Int32 nParticipantID, Int32 nSessionID) in C:\dev\app\server\Services\Decision.cs:line 426

  • Are you sure that the "Nullable object must have a value" is on the line you think it is? Can you post the stack trace? – skaz Mar 11 '11 at 00:44

1 Answers1

1

Ahh. You are casting dt to a DateTime, but you need to cast it to a nullable DateTime (DateTime?)

skaz
  • 21,962
  • 20
  • 69
  • 98
  • pd.dtAcceptance is a DateTime not a DateTime? SubSonic generated it as such which was why I was wondering if I needed to change something in the SubSonic template to allow for nullable dates – Jonathan Michaels Mar 11 '11 at 00:57
  • Sorry - yes - the template should be a nullable DateTime if the field allows for nulls. – skaz Mar 11 '11 at 01:00
  • heh, any idea where to change that? simple task getting worse and worse, but thanks for the directional change – Jonathan Michaels Mar 11 '11 at 01:02
  • Sorry - I don't know SubSonic well - I just saw it was an ORM so figured it worked like other generation tools. Don't you point it at the database and it creates the layer for you? Is the column in your database nullable? – skaz Mar 11 '11 at 01:07
  • oh yeah, it is. I hate SubSonic, it is to the point where I might just create a stored procedure for this and hook it in place because I've already spent way too much time on this. thanks for the help – Jonathan Michaels Mar 11 '11 at 01:15
  • What about the ORM stuff inside of Visual Studio? That can create the same layer for you. – skaz Mar 11 '11 at 01:20
  • OK, figured it out. pd.AdviceAcceptanceHasValue = false; SubSonic creates the boolean that if you set to false it sets the NULL to the db behind the scenes. Marking your answer as accepted since I took your time. – Jonathan Michaels Mar 11 '11 at 01:26