Hi all.
I've got a problem when using FormView + SqlDataSource as followings:
1. My environment: windows XP SP2, Region setting for format is based on German
(Switzeland), Database server: SQL Server 2005, IDE: MS Visual Studio 2005
2. In my ASPX Source for the issued page, I use a FormView for editing data from
Database. Among the control in the EditTemplate, I use a TextBox to bind a
DateTime field like this:
<asp:TextBoxID="txtCreateDate"Text='<%#
Bind("CREATE_DATE") %>'runat="server"
></asp:TextBox>
3. I've got a SqlDataSource contained an UpdateCommand with nothing special:
"UPDATE [Products] SET ....CREATE_DATE=@CREATE_DATE.... WHERE.....
[ID]=@ID"
and also the same with SelectCommand: "SELECT * FROM [Products] WHERE
ID=@ID"
4. In my code for the issued page, I override the InitializeCulture to make the
displayed DateTime has the format of Switzeland (that is: dd.MM.yyyy)
protected override void InitializeCulture()
{
string languageCode = "en";
string languageCulture = "de-CH";
Culture = languageCulture;
UICulture = languageCode;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(languageCulture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageCode);
base.InitializeCulture();
}
When the data is first load for a record from database to be ready for edit,
everything sounds ok and the TextBox txtCreateDate display a value like this:
30.04.2008 00:00:00
But when I edit the txtCreateDate to: 25.04.2008 00:00:00 or leave everything
no-changed, the error occured with this date time (I show the error was at the
end of this post).
I know what the error mean: valid format string for the updated datatime must be
04.25.2008 instead of 25.04.2008
I hope if anyone could help me to solve this problem while i need to display
dd.MM.yyyy in the form.
Thanks
(Below is the error detail)
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.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles
styles) +2271586
System.DateTime.Parse(String s, IFormatProvider provider) +26
System.Convert.ToDateTime(String value, IFormatProvider provider) +49
System.String.System.IConvertible.ToDateTime(IFormatProvider provider) +10
System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider
provider) +528
System.Web.UI.WebControls.Parameter.GetValue(Object value, String
defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean
ignoreNullableTypeChanges) +248
System.Web.UI.WebControls.SqlDataSourceView.AddParameters(DbCommand command,
ParameterCollection reference, IDictionary parameters, IDictionary
exclusionList, String oldValuesParameterFormatString) +547
System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys,
IDictionary values, IDictionary oldValues) +323
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values,
IDictionary oldValues, DataSourceViewOperationCallback callback) +78
System.Web.UI.WebControls.FormView.HandleUpdate(String commandArg, Boolean
causesValidation) +1154
System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +450
System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e)
+88
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs
e) +109
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +85
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
+155
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBa
ckEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint) +4921
|
The date format may be coming from the user settings in the control panel.
From the online help:
The user might choose to override some of the values associated with the current
culture of Windows through the regional and language options portion of Control
Panel. For example, the user might choose to display the date in a different
format or to use a currency other than the default for the culture. If the
CultureInfo..::.UseUserOverride property is set to true, the properties of the
CultureInfo..::.DateTimeFormat instance, the CultureInfo..::.NumberFormat
instance, and the CultureInfo..::.TextInfo instance are also retrieved from the
user settings.
|