1

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."

Piotr
  • 572
  • 5
  • 22

3 Answers3

0

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.

Jim Wolff
  • 5,052
  • 5
  • 34
  • 44
  • Thanks Frozen. Unfortunately I cannot change the type on SQL right now, but I have a feeling that differences between C# setup and SQL might be the case. Can you guide me where in the scope of my C# application I can set the culture (as I said I tried the `` to no avail)? – Piotr Oct 01 '13 at 11:30
  • `HttpServerUtility.UrlDecode` or [Decode escaped Url without using HttpUtility.UrlDecode](http://stackoverflow.com/questions/239567/decode-escaped-url-without-using-httputility-urldecode) could be helpful in this case – Jim Wolff Oct 01 '13 at 11:40
  • Thanks, I'll investigate on this. Meanwhile I've added >Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); >Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); to Site.master OnInit event. The exception disappeared but Details says "No such item.". The URL got this instead "Created=09%2F30%2F2013 16%3A10%3A33&Updated=09%2F30%2F2013 23%3A10%3A40" – Piotr Oct 01 '13 at 11:50
  • I've resolved it to the point where I don't get an exception (by changing the culture in web.config) but then the Details page doesn't find the item (I tried decoding the URL as you suggested, and in a similar way to presented here http://forums.asp.net/post/4854958.aspx, but that did not help). I guess I have to try to figure out more about how Dynamic Data works before I can fix this consciously... – Piotr Oct 02 '13 at 13:34
  • @Caleb9 Try looking into model binding, you can create your own custom modelbinder for datetime, i think i had exactly your problem at one point, it only occured when using a GET request and not a POST, since post was able to use the correct culture. Also you should actually always use invariant-culture format of the date in your get request, for example by using a hidden field that has the invariant date and submitting that, instead of the real field that has the culture specific representation. – Jim Wolff Oct 02 '13 at 13:55
0

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.

Adarsh Kumar
  • 1,134
  • 7
  • 21
  • Thanks Adarsh, can you please guide me to some tutorial or something where I can find out how to do that? As I said, this is my first Dynamic Data project and I don't know my ways around too well yet. – Piotr Oct 01 '13 at 11:32
  • @Caleb9: I would never suggest you to use DateTime datatype as primary key. It is always a bad practice. Use some other datatype like string, int etc and your problem will be resolved. – Adarsh Kumar Oct 03 '13 at 05:00
  • That is true and if you look at the way I resolved it, that was actually the problem. Unfortunately I can't change the table as it is used in other parts of the system, and it took me a while to figure out I can drop the Entity Key property for DateTime columns in EF model. Thanks for your help. – Piotr Oct 03 '13 at 12:00
0

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.

Piotr
  • 572
  • 5
  • 22