views:

159

answers:

0

Hi. I've been using subsonic 3.0 for the past few days and ran into a problem. Using ActiveRecord and calling save on the existing record caused:

[SqlException (0x80131904): The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.The statement has been terminated.]

The code I'm using is just to demonstrate the problem:

public void ButtonCreate_OnClick(object sender, EventArgs e)
{
    stPost post = new stPost();
    post.postCreatedDate = DateTime.Now;
    post.postDescription = "cool post at " + DateTime.Now.ToString();
    post.postGuid = Guid.NewGuid();
    post.postTitle = "Post title";
    post.postUpdatedDate = DateTime.Now;

    post.Save();
}

public void ButtonUpdate_OnClick(object sender, EventArgs e)
{
    stPost post = stPost.All().ToList<stPost>()[0];
    if (post != null && !post.IsNew())
    {                
        post.postDescription = "cool post UPDATED at " + DateTime.Now.ToString();                
        post.postTitle = "Post title Updated";
        post.postUpdatedDate = DateTime.Now;

        post.Save();
    }
}

Looking into sql profiler and resharper I found that insert and update statements are generated in completely different ways. On insert I'm getting the following sql code:

exec sp_executesql N'INSERT INTO 
[dbo].[stPost]([dbo].[stPost].[postGuid],[dbo].[stPost].[postCreatedDate],[dbo].[stPost].[postUpdatedDate],[dbo].[stPost].[postTitle],[dbo].[stPost].[postDescription],[dbo].[stPost].[deleted])
 VALUES 
(@ins_dbostPostpostGuid,@ins_dbostPostpostCreatedDate,@ins_dbostPostpostUpdatedDate,@ins_dbostPostpostTitle,@ins_dbostPostpostDescription,@ins_dbostPostdeleted)
;
SELECT SCOPE_IDENTITY() as new_id
; SELECT SCOPE_IDENTITY() as new_id',N'@ins_dbostPostpostGuid uniqueidentifier,@ins_dbostPostpostCreatedDate datetime,@ins_dbostPostpostUpdatedDate 
datetime,@ins_dbostPostpostTitle nvarchar(10),@ins_dbostPostpostDescription nvarchar(32),@ins_dbostPostdeleted 
bit',@ins_dbostPostpostGuid='91695935-588B-4617-8DB0-14210E97F718',@ins_dbostPostpostCreatedDate=''2009-07-14 
14:11:52:997'',@ins_dbostPostpostUpdatedDate=''2009-07-14 14:11:52:997'',@ins_dbostPostpostTitle=N'Post title',@ins_dbostPostpostDescription=N'cool 
post at 14.07.2009 14:11:52',@ins_dbostPostdeleted=0

And on update

exec sp_executesql N'UPDATE [stPost] 
 SET postDescription=@up_postDescription, 
postTitle=@up_postTitle, 
postUpdatedDate=@up_postUpdatedDate
 WHERE [dbo].[stPost].[postID] = @0',N'@up_postDescription varchar(40),@up_postTitle varchar(18),@up_postUpdatedDate varchar(19),@0 
int',@up_postDescription='cool post UPDATED at 14.07.2009 14:11:58',@up_postTitle='Post title Updated',@up_postUpdatedDate='14.07.2009 14:11:58',@0=2

On insert and update date is passed in the different ways. And parsing '14.07.2009' results in out-of-range value. I guess the problem might be somewhere in cultural/globalization settings because I have my local culture set to Russian, and sql server collation is set to Cyrillic_General_CI_AS. But the problem doesn't occure on insert, only on updates. And this makes me think that problem is somewhere in subsonic. Any help or ideas will be very appreciated.

Vladimir