SqlDateTime overflow. Must be between 1/1/1753 12:... ADO.NET EntityFramework

D

Deshi

This strange error bugs me lot.... This is very strage, I'm trying to
insert data into the table through ADO.NET Entity Framework.. nothing
seems to work my way.. i've been getting error "SqlDateTime overflow.
Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM" .

here is my code

DirectoryEntities bdEntities = new DirectoryEntities();


Directory_SubCategory bdSubCatergories = new Directory_SubCategory();
Directory_Category categoryObject = new Directory_Category();
bdSubCatergories.Directory_Category = categoryObject;
bdSubCatergories.SubCategoryName = masterParams[0];
bdSubCatergories.Directory_Category.CategoryID = Convert.ToInt32
(masterParams[1]);
bdSubCatergories.CreatedBy = Convert.ToInt32(masterParams[2]);
bdSubCatergories.CreatedDate = DateTime.Now; ///This could be
source of error
bdSubCatergories.ModifiedDate = DateTime.Now;
bdEntities.AddToBusinessDirectory_SubCategory(bdSubCatergories);
bdEntities.SaveChanges(true); //It throws me exception here

All my previous database insertions are happening perfectly. in this
case i'm getting strange exception. reason could be i'm mapping two
table to update.. does anyone had this kind of error
 
A

Andrew Morton

Deshi said:
This strange error bugs me lot.... This is very strage, I'm trying to
insert data into the table through ADO.NET Entity Framework.. nothing
seems to work my way.. i've been getting error "SqlDateTime overflow.
Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM" .

It seems unlikely that DateTime.Now would be outside the acceptable range. I
don't know where in your code the actual SQL parameters are set up, but I
check the value that's going to be added against the limits like this:

sqlParam = New SqlParameter("@DateTaken", SqlDbType.DateTime)
If reader.DateTaken >= CType(SqlDateTime.MinValue, DateTime) AndAlso
(reader.DateTaken <= CType(SqlDateTime.MaxValue, DateTime)) Then
sqlParam.Value = reader.DateTaken
Else
sqlParam.Value = DBNull.Value
End If
sqlcmd.Parameters.Add(sqlParam)

(where reader is a class which extracts exif data from a jpeg file and
DateTaken in the sql table allows nulls).

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top