Ok, for now I've fixed this problem by defining a custom type handler since 
IBatis is expecting a CLR type of DateTime (code below).  But somebody please 
tell me there is a better way of doing this.  Isn't IBatis smart enough to know 
that the date is being casted to a string and behave accordingly?

        public class SmallDateTimeTypeHandler  : ITypeHandlerCallback
        {
                public SmallDateTimeTypeHandler() {}
                public object ValueOf(string nullValue) {
                        return nullValue;
                }

                public object GetResult(IResultGetter getter) {
                        if (getter != null) {
                                DateTime dt = (DateTime) getter.Value;
                                return dt.ToString();
                        } else {
                                return null;
                        }
                }

                public void SetParameter(IParameterSetter setter, object 
parameter) {
                        string val = (string) parameter;
                        setter.Value = val;
                }
        }



>  -------Original Message-------
>  From: Zarar Siddiqi <[EMAIL PROTECTED]>
>  Subject: smalldatetime to string conversion problem in sql server
>  Sent: 13 Mar '06 15:53
>  
>  Hi,
>  
>  I'm having problem reading a column which stores a date as smalldatetime in 
> SQL Server.  I can write to the column fine using a string property but when 
> I try to read it, I get the error pasted below.    I've already tried to 
> specify the types explicitly but that didn't work:
>  
>  <result column="ShipDate" property="ShipDate" type="string" 
> dbType="SmallDateTime"/>
>  
>  The reason I know it's this column that's the problem is because as soon as 
> I take it out, everything starts to work again.
>  
>  Any help is apprecited.  
>  
>  Here's the error I get:
>  
>  System.InvalidCastException: Specified cast is not valid.
>  
>  With the trace:
>  [InvalidCastException: Specified cast is not valid.]
>     System.Data.SqlClient.SqlDataReader.GetSqlString(Int32 i) +46
>     System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +32
>     
> IBatisNet.DataMapper.TypeHandlers.StringTypeHandler.GetValueByName(ResultProperty
>  mapping, IDataReader dataReader) +79
>     
> IBatisNet.DataMapper.Configuration.ResultMapping.ResultProperty.GetDataBaseValue(IDataReader
>  dataReader) +32
>     
> IBatisNet.DataMapper.MappedStatements.MappedStatement.SetObjectProperty(RequestScope
>  request, ResultMap resultMap, ResultProperty mapping, Object& target, 
> IDataReader reader) +235
>     
> IBatisNet.DataMapper.MappedStatements.MappedStatement.ApplyResultMap(RequestScope
>  request, IDataReader reader, Object resultObject) +241
>     
> IBatisNet.DataMapper.MappedStatements.MappedStatement.RunQueryForObject(RequestScope
>  request, IDalSession session, Object parameterObject, Object resultObject) 
> +133
>     
> IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject(IDalSession
>  session, Object parameterObject, Object resultObject) +92
>     
> IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject(IDalSession
>  session, Object parameterObject) +10
>     IBatisNet.DataMapper.SqlMapper.QueryForObject(String statementName, 
> Object parameterObject) +147
>  
>  
>  

Reply via email to