DateTime value getting changed during conversion

Posted on 2007-11-28
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
  • 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 ]
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


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 25 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

726 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