-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Saving object with a optional DateTime property
PostPosted: Sat Jun 04, 2005 6:47 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I have an object which has a property of the type DateTime which can be left empty.
I fill the datetime property like this:
Code:
if (txtDate.Text != "" && DateTime.Parse(txtDate.Text) != DateTime.MinValue)
{
   _object.Date = DateTime.Parse(txtDate.Text);
}

When I leave txtDate empty, the DateTime property is not filled.

But when I save the object I get an exception:
Code:
+   _innerException   {"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM." }   System.Exception


How do I handle this situation correctly?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 10:15 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
you have to use the Nullables.NHibernateNullableDateTime property in your POCO:

Code:
using Nullables; // NHibernate-specific...

namespace POCO.Classes {
  public class Foo {
    private NullableDateTime yourDateMember;

    public virtual NullableDateTime YourDateMember{
      get { return this.yourDateMember; }
      set { this.yourDateMember= value; }
    }
  }
}


Mapping File:
Code:
<property name="YourDateMember" column="YourDateMember" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />


If you are already using this, then you are probably using an older version of NH. I think this bug in Nullables was fixed in .0.8.2.

HTH,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 12:07 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
The compiler can't find the namespace Nullables or the class NullableDateTime. And neither can I...
I just downloaded NHibernate 0.8.4.
Do I have to download a separate library or so?

Greets,
Jack


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 2:07 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, NHibernateContrib.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 4:44 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I'm using the NullableDateTime type for my Date-property mentioned above. But when I try to save the object, I'll get an exception with the following innerexception: "Object must implement IConvertible". Has this exception something to do with using NullableDateTime?

My mapping file looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyApp.Core.Domain.Customer, MyApp.Core" table="customer">
      <id name="Id" type="Int32" column="customerid" unsaved-value="-1">
         <generator class="identity"/>
      </id>
      
  <property name="Date" column="date" type="Nullables.NullableDateTime, Nullables"/>
   ... rest of properties
   </class>
</hibernate-mapping>


Greets,
Jack


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 6:06 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
it might, but i'm not sure. can you post your class file (member declaration, property declaration, and constructors)?

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 5:17 am 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I already solved this one. I mapped some properties to the wrong types.
Thanx anyway for the NullableDateTime suggestion.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 2:41 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I've got still 2 issues:

1. The datetime field in the database is filled with '1-1-1900 0:00:01' when I leave the NullableDateTime property empty.. Is there a way to make sure that this field stays empty when I don't fill in a date? If not, it's not a big problem, but I was just wondering. I'm using MSSQL.

2. When NHibernate tries to read the objects with the NullableDateTime property, I'll get an exception. The ADOException is: 'Could not cast the value in field date2_ of type DateTime to the Type SerializableType. Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.'

My mapping file looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyApp.Core.Domain.Customer, MyApp.Core" table="customer">
      <id name="Id" type="Int32" column="customerid" unsaved-value="-1">
         <generator class="identity"/>
      </id>
      
      <property name="Date" column="date" type="Nullables.NullableDateTime, Nullables"/>
   </class>
</hibernate-mapping>


Is there something wrong with my mapping of the date property?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 7:11 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
there are two library files that pertain to NH in the NHContrib. make sure your application is referencing Nullables.dll and not Nullables.NHibernate.dll.

However your mapping file must reference Nullables.NHibernate.NullableDateTimeType:

Mapping file:
Code:
<property name="LastLogin" column="LastLogin" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />


Class file:

Code:
using Nullables; // NHibernate-specific...

namespace AMA.Core.Domain
{
   /// <summary>
   /// Summary description for Member.
   /// </summary>
   public class Member: IIdentity, IComparable
   {
      private NullableDateTime lastLogin;
...



I'm not sure if this is exaclty your problem, but this is how I set it up and I get NULLs in my DB and no exceptions.

-devon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.