Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


DateTime value getting changed during conversion

Posted on 2007-11-28
Medium Priority
Last Modified: 2013-11-26

Dear All,

I am having a web form in which I want to give the user the functionality of choosing a date using the calender control.

For this I created a web form called calender.aspx in which I have a Calender control, a label and a textbox and button.In the SelectionChanged Event I have written code which shows the date selected by the user on the Label and textbox.Then when the button is pressed the date value is sent to another web form using the technique of cross-posting.

In the receiving page I have the following code written in the page_load event in order to receive the date value sent from calender.aspx.


protected void Page_Load(object sender, EventArgs e)


if (Page.PreviousPage != null)


txtcrtn_date = (TextBox)PreviousPage.FindControl("Textbox1");

crtndate = DateTime.Parse(txtcrtn_date.Text.ToString());


lblselectDate.Text = crtndate.ToShortDateString();

Response.Write("the date is" + crtndate.ToShortDateString());

crtndate1 = DateTime.Parse(lblselectDate.Text.ToString().Trim());

//crtndate1 = crtndate1.ToShortDateString();

moddate = crtndate1;

lblmodDate.Text = moddate.ToShortDateString();



The variables crtndate, crtndate1, moddate are all variables of type DateTime.

As you can see I am converting the value received from previous page i.e calender.aspx into a DateTime type and assigning it to the variable crtndate. Then this value is displayed in a Label lblSelectDate.

The problem is that the variable crtndate has a value of ' 1/1/2007 12:00:00 AM'  - I don't know how this value is coming in here. But the value in the Label lblSelectDate is CORRECT. For eg, if I select 11/24/2007 it is displayed in the Label but gets changed in the crtndate variable.

But I need to get the CORRECT date in the variables crtndate, crtndate1 even after conversion because I need to insert these values into a table which has 2 DateTime fields. Now when I try to insert I get the following error:

System.Data.SqlTypes.SqlTypeException was unhandled by user code
  Message="SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."

This message is obvious bec' the date 1/1/2007 is outside the range of SQL DateTime data type.

How to tackle this conversion problem?

I tried several things but it didn't work.
Another question: If I have a DateTime value in my web form do I need to have same datatype in the Sql Server table column or can I have a smalldatetime column in the database Table.

Any help will be extremely appreciated.

Thanks in advance for you time.
Question by:coolmind26
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2

Expert Comment

ID: 20365622
Are you posting to the second web form? If so, why not just use Request.Form["FieldName"].ToString() and then convert to a date/time?

Also, on you question regarding a date/time value in the web form and smalldatetime data type, that will work fine, the only difference is that smalldatetime  has less precision than datetime:

Date and time data from January 1, 1900, through June 6, 2079, with accuracy to the minute. smalldatetime values with 29.998 seconds or lower are rounded down to the nearest minute; values with 29.999 seconds or higher are rounded up to the nearest minute.

Expert Comment

ID: 20365637
What result does the line:

Response.Write("the date is" + crtndate.ToShortDateString());

produce (exactly)?

You say the date is showing up as "1/1/2007 12:00:00am"???  How is that outside of the SQL DateTime data type range???

Author Comment

ID: 20367592

I made a mistake. The DateTime variables crtndate, crtndate1 are having a value of '1/1/0001 12:00:00AM' which is outside the range of SQL Server DateTime datatype.

smalldatetime     -     [1/1/1900    to 1/1/2079]

DateTime        -        [1/1/1753    to    1/1/9999 ]
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.


Expert Comment

ID: 20367940
It sounds like your variable isn't actually pulling the value from your previous page...

Expert Comment

ID: 20367972
Both crtndate AND crtndate1 are getting that bogus value???  I could see where maybe crtndate1 might be getting a messed up value....  You get the value for crtndate first from the other form, and this appears to work correctly because when you assign the value to the label, you said the label displays correctly (right?).  But then, you are taking that Sort Date String value from the recently assigned label and assigning it to a DateTime field.

I don't follow your code logic... you keep reassigning the same date value to multiple variables.  Why not just use the ONE variable?

crtndate = [parsed date specified in the previous form]
label = crtndate (formatted as Short Date string)
crtndate1 = label (formatted BACK into a DateTime value !!! <-- potential problem !!!)
moddate = crtndate1
moddateLabel = moddate (formatted as Short Date string)

??? Doesn't make sense. Once you've parsed the textbox value from the previous form into a DateTime variable, just use that ONE variable for the rest of the code.  As it is, you're attempting to assign essentially the same value (in different forms) to 5 different variables/controls.

Author Comment

ID: 20380181

Hi Garbi4332,

Let's not get into issue of assigning a single value to multiple variables/controls. It's NOT the problem here.
The problem here is to create a DateTime variable which has the format of a short date i.e 11/24/2007 or 24/11/2007.

I solved the problem partially. Given below is how.

  txtcrtn_date = (TextBox)PreviousPage.FindControl("Textbox1");// it is getting the date value from previous page to the Textbox txtcrtn_date

string mydate = txtcrtn_date.text.ToString();
Response.Write("the date is" + mydate); // when I see the value of mydate I find that it is exact value selected in the previous page i.e 11/24/2007

Uptil now  it is OK.
Now the problem starts.When I try to convert it to a DateTime variable I get the general form of datetime

crtndate = DateTime.Parse(mydate);
Response.Write("the crtndate is" + crtndate);//  Now I get a value of 11/24/2007 12:00:00 AM

But I don't want the time part I want only 11/24/2007 in the Datetime variable. I am not able to get this despite all my efforts.

In order to get only date part I changed my initial part like this.

txtcrtn_date = (TextBox)PreviousPage.FindControl("Textbox1");

 crtndate = DateTime.ParseExact(txtcrtn_date.Text.ToString(), "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);

and now when I do a Response.Write to view the value of crtndate I see it is 11/24/2007 12:00:00 AM

I tried several other things but none gave me the desired result. I want only the date part in a DateTime variable.
I am doing this in ASP.NET 2.0.


Expert Comment

ID: 20381934
You should be able to add .ToShortDateString() to you line:

crtndate = DateTime.Parse(mydate);

crtndate = DateTime.Parse(mydate).ToShortDateString();

Author Comment

ID: 20387991
Hi digitalthoughts,

I tried that earlier, it didn't work.

I tried it again , won't work. I also used TryParse() and TryParseExact() also but it didn't work.

Accepted Solution

digital_thoughts earned 100 total points
ID: 20395710
Ok, that's extremely odd... I guess you could to a rather crude option which is just a replace:

.Replace(" 12:00:00 AM","")

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question