SqlDateTime overflow error

I have an update method that is calling down into my data layer, which is a typed dataset.  When I attempt to update and leave one of the fields "terminationdate" empty in my gridview (it is a nullable field), I get the following error message:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

See my method code below.

The field should accept a null value.

Thanks for any help.
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdateUser(string firstname, string lastname, string middleinitial, string phone, string email, string alternatephone, string ssn, string costcenter, int status, DateTime terminationdate, string notes, int userid)
        {
            TechCenter.UsersDataTable users = Adapter.GetUserByUserID(userid);

            if (users.Count == 0)
                return false;

            TechCenter.UsersRow user = users[0];

            user.FirstName = firstname;

            user.LastName = lastname;

            if (middleinitial == null)
                user.SetMiddleInitialNull();
            else
                user.MiddleInitial = middleinitial;

            if (phone == null)
                user.SetPhoneNull();
            else
                user.Phone = phone;

            if (email == null)
                user.SetEmailNull();
            else
                user.Email = email;

            if (alternatephone == null)
                user.SetAlternatePhoneNull();
            else
                user.AlternatePhone = alternatephone;

            if (ssn == null)
                user.SetSSNNull();
            else
                user.SSN = ssn;

            if (costcenter == null)
                user.SetCostCenterNull();
            else
                user.CostCenter = costcenter;

            user.Status = status.ToString();

            if (terminationdate == null)
                user.SetTerminationDateNull();
            else
                user.TerminationDate = terminationdate;

            if (notes == null)
                user.SetNotesNull();
            else
                user.Notes = notes;

            int rowsAffected = Adapter.Update(user);

            return rowsAffected == 1;

        }

Open in new window

-Dman100-Software ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
Are you setting the date to DateTime.MinValue somewhere in the code and then trying to write that out to the database?  That will cause a problem because DateTime.MinValue is 1/1/0001 (or something like that) and the minimum SQL datetime is 1/1/1753, so you'll get this error.

You need to ensure it's really either a real DB NULL or set it to 1/1/1753 instead of DateTime.MinValue.
0
-Dman100-Software ConsultantAuthor Commented:
The only place I am setting the value in code is in my update method:

if (terminationdate == null)
                user.SetTerminationDateNull();
            else
                user.TerminationDate = terminationdate;

In my gridview, I'm just leaving the termination field blank, which should branch into the if statement where terminationdate equals null and call SetTerminationDateNull().

SetTerminationDateNull() wouild be DB Null, correct
0
Shaun KlineLead Software EngineerCommented:
DateTime variables do not have a null value. If you want to have a nullable DateTime variable, use the DateTime? data type.

You can then check if the variable is null by using the <variable>.HasValue property.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Dhanasekaran SengodanCommented:
try this

if (terminationdate == null)
                user.TerminationDate ="1/1/1900";
            else
                user.TerminationDate = terminationdate;
0
cyberkiwiCommented:
Hi there,

The problem will be in SetTerminationDateNull() can you show it?
It should be something simple as user.TerminationDate = DbNull.Value instead of
user.TerminationDate = <xxx>

where <xxx> is out of the valid date range
0
dukestaTAICommented:
Read this : http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldatetime.minvalue.aspx

Namespace:  System.Data.SqlTypes
Assembly:  System.Data (in System.Data.dll)

Try using SqlDateTime.MinValue
0
-Dman100-Software ConsultantAuthor Commented:
Changing the terminationdate datatype to DateTime? for my parameter and then updating the lines where I set the value to the following:

if (terminationdate == null)
                user.SetTerminationDateNull();
            else
                user.TerminationDate = (DateTime)terminationdate;

Worked!

Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.