views:

26

answers:

2

Hi, I have developed the asp.net mvc 2 + C# application. it has create functionality in which there is datetime field. If locally I runs the a application it is working fine . but if i deploys it on IIS server (v 5.1) it, is not accepting date more than 12 (if gives 14/10/2010) will not work), this means it is changing the date format at run time on iis as MM/dd/yyyy. But in insert code I am converting the format in "dd/MM/yyyy" by this line :

objRecord.NextElectionDate = DateTime.ParseExact(collection["NextElectionDate1"], "dd/mm/yyyy", null);

I checked the regional settings , it is same as where i locally running application (US cultured) .Not any exception getting out.Also I tried to deploy application on iis 6.0 , where it is running perfect. But on client side there is IIS 5.1 version available so the application must be run somehow on IIS 5.1. Is this date problem cause of the IIS version. How to resolve this issue ?

Edited 1: alt text

I have code like this :

alt text

Edited 2: Hi again ! while I am debugging the application I am getting ModelState.IsValid is "false" ,while trying to insert officer information on "Create" Action . how to know what is going wrong in ModelState.IsValid ? why should it is getting "false" ? Please guide

+1  A: 

Try specifying the culture and uiCulture in your web.config.

Also it would be better to use default model binder and action parameters instead of manually parsing dates:

public ActionResult Foo(DateTime? nextElectionDate1)
{
    // no need to parse parameters
    return View();
}
Darin Dimitrov
+1  A: 

Your problem has nothing to do with IIS. It is a classic database datetime formatting issue.

More than 12 gives you the answer: there are only 12 months in the year! So:

Do NOT format the date in the "insert code" to anything other than US format, as your database storage will expect the dates in US format.

I have seen this time and again with MS Access databases.

The simple solution is ALWAYS ensure that the dates you use in your Edit and Insert code have been formatted for US culture, ie, mm/dd/yyyy.

Darin Dimitrov is right about the Model Binder practice he mentions. But the more serious issue here is a square peg in round hole one: As Microsoft is a US company, their databases want dates in US format.

In MS Access, if the data input goes through a form, you are fine just relying on your PCs culture settings. However, if you insert data using VBA code, you need to explicitly format dates in US format.

That is exactly what is happening here.

awrigley
any updates over this question ? It is too important for all developers I think !
Lalit
Lalit: we don't have enough information from you. Maybe it is not important enough for you to make the necessary effort?
awrigley
Ok, but, one thing i got yet. i think this error is run time and i think it is thrown by Model in my mvc application. I am trying on my best but getting disappointment ! please guide .
Lalit
This is not working for me. but though i am accepting this as answer, have no choice !!!!!!!!!!!!!!! :(
Lalit