I am developping a small training ASP .Net web application.
I have developped an ASPX page where I have declared a FormView control linked to an ObjectDataSource through data binding.
The update method takes a Product object.
A Product object has a property Price containing a double value.
The user can Create Read Update Delete products through the FormView.
But an error can be raised when updating or inserting a product.
More precisely the error is raised when a Product object is instantiated from input values of the FormView.
The string contained in the input control associed with the Price property could not be cast to a double.
As an example “50,6” can not be cast to a double.
The coma is the decimal separator.
I guess ASP .Net expects a point for the decimal separator because it probably considers the EN-US culture.
Here is the stack trace I get in the browser :
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +489
System.Web.UI.WebControls.ObjectDataSourceView.ConvertType(Object value, Type type, String paramName) +117
System.Web.UI.WebControls.ObjectDataSourceView.BuildObjectValue(Object value, Type destinationType, String paramName) +167
System.Web.UI.WebControls.ObjectDataSourceView.BuildDataObject(Type dataObjectType, IDictionary inputParameters) +229
System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +1421
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +95
System.Web.UI.WebControls.FormView.HandleUpdate(String commandArg, Boolean causesValidation) +1154
System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +445
System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e) +112
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Is it possible to specify a culture to consider when setting object properties from string values through data binding ?
For .Net 4.5 and above this may work:
Also, make sure the test client’s culture is set to “FR-FR” for both browser and OS. The MSDN explains that the client’s culture may be used instead of the server’s culture for parsing. (They don’t discuss invariant culture but you definitely don’t want it.)
If this doesn’t work, or you’re on .NET 4.0 or earlier using InsertParameters may help (regardless of whether you’re using POCOs or parameters):
see this thread: http://forums.asp.net/t/963913.aspx/1
EDIT: There’s a related stack overflow question on this: ObjectDataSource fails to parse string to DateTime