DateTime value getting changed during conversion
Posted on 2007-11-28
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.