TypeConverter nullable DateTime to TimeSpan

Posts   
 
    
gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 29-Apr-2011 20:00:10   

LLBLGen Pro 3.1 Final (February 7, 2011) Sql Server 2008 R2 Visual Studio 2010 .NET 3.5 Framework

Hi,

I'm have a nullable Time field in my database that I want to convert to nullable DateTime using the TypeConverter. I need this because my application time picker binds on Date not TimeSpan. I can save back into the database fine if I provide a value, but fails if I don't provide anything (resulting in null).

The ConvertTo method will throw ArgumentNullException because the value is null. I tried to replace it with a "return null" to bypass it, but then my save entity errors out with a "Value can't be null". What am I doing wrong here if I want to save a null back to the DB?

public class DateTimeToTimeConvertor : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (sourceType == typeof(TimeSpan));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (destinationType == typeof(TimeSpan));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null || value == DBNull.Value)
                return null;

            if (value is TimeSpan)
            {
                TimeSpan t = (TimeSpan)value;
                return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, t.Hours, t.Minutes, 0);
            }

            throw new NotSupportedException("Conversion from a value of type '" + value.GetType().ToString() + "' to System.DateTime isn't supported");
        }



        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
                throw new ArgumentNullException("value", "Value can't be null");
            if (!(value is DateTime))
                throw new ArgumentException("Value isn't of type DateTime", "value");

            DateTime d = (DateTime)value;
            TimeSpan t = new TimeSpan(d.Hour, d.Minute, 0);
            System.Diagnostics.Trace.WriteLine("DateTime = " + value.ToString() + " converted to timespan = " + t.ToString());
            return t;
        }


        public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
        {
            return new DateTime();
        }
    }

Thanks! Gilbert

gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 29-Apr-2011 21:01:23   

I found the problem. Returning null in ConvertTo does indeed seem to work. For some reason I had an older version of it in my GAC still throwing the exception flushed Not sure how it got in there. Took me awhile to realize it.

Thanks and sorry for the inconvenience.