Question

I am trying to build a simple website using an "ASP.NET Dynamic Data Entities Web Application" project template in VS 2012 (C#). It is pretty much the standard template setup, with an entity where DateTime type property is part of a primary key (the underlying table has also the SQL DateTime type on the mapped column). I get FormatException when I try to see the Details page, or edit a row:

String was not recognized as a valid DateTime. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FormatException: String was not recognized as a valid DateTime.]
System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +965
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
System.Web.UI.WebControls.FormView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105 System.Web.UI.WebControls.FormView.EnsureDataBound() +178
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +83 System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

I tried setting the culture in <globalization> node in web.config but it did not help. The URL for Details page contains the columns in query string, looking like this: &Created=09%2F30%2F2013 16%3A10%3A33&Updated=09%2F30%2F2013 23%3A10%3A40 for a row where Created is '30-09-2013 16:10:33' and Updated '30-09-2013 23:10:40'.

It is my first Dynamic Data project so I do not really know what to do... Thanks in advance for any help.

Edit:

Adding

<globalization uiCulture="en-US" culture="en-US"/>

to system.web node of the main web.config caused the exception to disappear, but now the Details can't find the correct row, saying "No such item."

Was it helpful?

Solution 3

I managed to resolve the issue. Turns out the underlying table did not have primary key defined, so all the NOT NULL columns (including two DateTime columns) inferred Entity Key properties in the Entity Framework entity for that table. After setting "Entity Key" property to False for these two properties everything started to work as it should.

This is not a universal solution, but more of a workaround for this particular case, where rows are uniquely identified by other columns than those with DateTime type. If I really had a table where DateTime column would be the primary key, I guess I would run into the same issue again.

Regarding FormatException, setting <globalization culture="en-US" /> in web.config under system.web node fixed it, but after removing the Entity Key property from the DateTime columns even this was unnecessary, since DateTime values were not used to construct the URI anymore.

OTHER TIPS

Since you have defined datetime as primary key, it is url-encoded by browser while sending get request. You should decode this value in your view before using it. Framework is trying to parse the encoded value as datetime and because of this giving exception.

You could be having localization issues, the datetime format can be different for different countries and also different between what you use in c# and sql

Try submitting in invariant format like yyyy-mm-dd

Also you should use datetimeoffset as your datetime instead, it has more precision and takes the same space if i remember correct, or at least use datetime2 as your sql data type since that has more precision and takes the same or less space.

Update: It might be an issue with parsing the values as Adarsh suggests, you can change your culture programatically:

// Put the using statements at the beginning of the code module
using System.Threading;
using System.Globalization;
// Put the following code before InitializeComponent()
// Sets the culture to French (France)
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
// Sets the UI culture to French (France)
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

Taken from msdn

You can try that to see if that is perhaps the issue, then you can look into setting it from config or maybe look at different modelbinding for datetime, there are several solutions depending on what is right for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top