Nick Kewney

Software Architect & Product Builder

DateTime.MaxValue overflow error in SQL Server CE

The following lines of code should pass the maximum date  that can be selected to a SQL statement via a parameter.

SqlCeCommand insertCommand = dbConn.CreateCommand();
insertCommand.CommandType = CommandType.Text;
insertCommand.CommandText = "INSERT INTO MyDateTable (DateField) Values (@MyDate)";
insertCommand.Parameters.Add(new SqlCeParameter("MyDate", SqlDbType.DateTime));
insertCommand.Parameters["MyDate"].Value = DateTime.MaxValue
insertCommand.ExecuteNonQuery();

This works fine on all desktop versions of SQL Server, but on SQL Server CE on Windows Mobile returns ‘DateTime.MaxValue overflow’ A workaround for this (not ideal by any means) can be achieved using:

DateTime.MaxValue.AddMilliseconds(-1);

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on whatsapp
WhatsApp

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.