Link to home
Start Free TrialLog in
Avatar of coolmind26
coolmind26

asked on

DateTime value getting changed during conversion



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());


//txtcrtn_date.Text

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.
Avatar of digital_thoughts
digital_thoughts

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.
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???
Avatar of coolmind26

ASKER

Hi,

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 ]
It sounds like your variable isn't actually pulling the value from your previous page...
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.

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.





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

crtndate = DateTime.Parse(mydate);


crtndate = DateTime.Parse(mydate).ToShortDateString();
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.
ASKER CERTIFIED SOLUTION
Avatar of digital_thoughts
digital_thoughts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial