This project is read-only.

DataTime function not working

Topics: Developer Forum, User Forum
Apr 15, 2009 at 4:09 AM
Hello everyone

  I have an odd  issue,  I have a small set of code in a usercontrol that will not work correctly in side the MWPSK easy contorl was hoping one of you might have some insight on how to resolve my issue.  I have spent a couple hours wondering why my code didn't work, so after running in Debug mode many many times, I finally created a new web-site with one page one one usercontorl and put a textbox and a button on the thing and walked through it in the debugger and the code worked fine, so I copied it back into my usercontrol and tried again and of course it didn't work or I would not have come here for help.   What on earth might make the code break inside MWPSK?   thanks tons for any help.


        DateTime today = new DateTime();
        string ExpirationDate = tb_ExpirationDate.Text;

        try
        {
            today = DateTime.Parse(ExpirationDate).Date;
        }
        catch
        {
         }

or this version

     try
     {
             today = Convert.toDateTime(ExperationDate);
     }
     catch
      {
       }


Apr 15, 2009 at 6:32 PM
Edited Apr 15, 2009 at 6:37 PM
Hi,

Are you getting any error messages, (either on-screen, or in App_Data/Error.log)?

My first impression, based on the details above, is that you may be encountering a parsing issue, due to culture-based problems with the input string.  Try explicitly providing a CultureFormat object, to see if this helps.

Example:

DateTime.Parse("12/05/2009", Thread.CurrentThread.CurrentCulture)

Depending on wheter you're current culture is GB, or US, you'll get different dates for this Parse, (12 May, or 5th Dec). 

This might not be your ultimate problem, but it's worth bearing in mind.

Eitherway, check for errors in the log - you might get more info regarding the problem.  Also, catch your errors with something - at the moment, any problems in the Parse operation are effectively ignored.  There's a Log class in the CMS you could use as a basis for writing some file-based logging, or at worst, you should pass the error up the call stack to a handling class, (that perhaps writes a message to the webpage).


hth,

Jim.
Apr 16, 2009 at 2:07 AM
Edited Apr 16, 2009 at 2:10 AM
Thanks Jim,

  I adding the CurrentCulture thing and tried again, and got the same issue.  the string had this value while viewing in Debug session  "8/15/2009"   as this is in a try catch area there was no error on the screen but this was in the exception log 

String was not recognized as a valid DateTime.

I use this code in other web site including DotNetNuke but this is the only place it doesn't work.  I know it has to do with site somehow but for the life of me I can't figure it out.  I am running on Vista 64bit in WebExpress 2008 and upload the site to a hosting provider and it doesn't work there either.  Even more in my catch statement I added 

today =

DateTime.Today.AddYears(1);

 

 

 

and that part works fine.  here is the whole button click event if it helps anyone see an issue. the top of the user control uses these

 

using 

 

System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit.Controls;
using System.Web.Security;
using System.Data;
using System.Linq;
using System.Threading; 

 

protected 

 

void btn_Save_Click(object sender, EventArgs e)
{

 

 

 

   decimal d_Category = 0;    
    DateTime today = new DateTime();
    string ExpirationDate = tb_ExpirationDate.Text;

 

 

 

    try
    
{
        today =
DateTime.Parse(ExpirationDate, Thread.CurrentThread.CurrentCulture).Date;
    } 
    catch
    {
        today =
DateTime.Today.AddYears(1);
    }  


Apr 16, 2009 at 11:43 PM
Hi,
It's definitely a parse error, based on the error message.

My guess is, it's seeing the data as 8th day of 15th month, which results in a failed parse.

There's a mismatch between the culture format of the dates you're using, (which looks US), and the CurrentCulture that MWPSK is running under, (my guess is UK). 

The reason you don't get this issue elsewhere is likely because the culture of the site, (e.g. DNN), matches the format of your dates. 

As for fixing the issue, ensure the website language, (on the CMS Setup page - Website.aspx) is set to "English (United States)", and use the method I outlined previously to parse dates, and all should work.

hth,

Jim.
Apr 17, 2009 at 4:29 AM
yes, yes, Yes!!!!   at least that is what my wife said... ok it was after I renewed the credit card.. .. Anyway that worked, I totally missed the CMS setting, I saw English and just skipped over it.  Thanks so much...
Apr 20, 2009 at 3:07 PM
np :)