I've run into a problem where a Dynamic Data page won't perform the server-side portion of the validation unless you submit via the controls in the details view. For example, I start with the Northwind database and a clean Dynamic Data project, create
a Custom Page for the Orders table, and add a button below the DetailsView:
Then I go ahead and put some bad date value (e.g. "aaa") in the Order Date field and try to submit using the Button (not the dynamic "Update") link.
If I do this in Firefox, nothing happens--the page submits, but nothing changes and the data is not updated.
In IE, similar, but I get a JavaScript error: "Sys.WebForms.PageRequestManager.ServerErrorException: Failed to set one or more properties on the type Order. Cannot convert value of paramter 'OrderDate' from 'System.String' to 'System.DateTime'" Weirdly, the
Javascript error seems to come after the postback.
When I run this in the debugger, I get a "LinqDataSourceValidationException was unhandled by user code":
"Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.DateTime'."
In none of these cases does the validation error message show on the page, and both of the .IsValid tests are true.
All other kinds of client-side validation I've tried--length, required, Int--seem to work okay with the Button submit method.
If I submit using the DetailsView "Update" linkbutton, the date validation works as expected.
I'm running the 5-23a runtime and templates, VS2008SP1Beta1 and .NET Framework 3.5SP1 (Beta) on a Windows Server 2003 VM that has only been used for this Dynamic Data project.
I did try that. Then I just get the same error sent to the browser that I was already seeing in the debugger:
Server Error in '/Northwind4' Application.
--------------------------------------------------------------------------------
Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.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.Web.UI.WebControls.LinqDataSourceValidationException: Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.DateTime'.
Source Error:
Line 48: if (DetailsViewValidator.IsValid && Page.IsValid)
Line 49: {
Line 50: DetailsView1.UpdateItem(true);
Line 51: }
Line 52: }
<div mce_keep="true">If I put an incorrect number in say a32.3800 into Freight and click the button I get the correct response, a validation error:
The field Freight must be a valid Double.</div>
<div mce_keep="true">If do the same thing with a date I get a runtime error:</div>
Failed to set one or more properties on type Order. Cannot convert value of parameter 'ShippedDate' from 'System.String' to 'System.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.Web.UI.WebControls.LinqDataSourceValidationException: Failed to set one or more properties on type Order. Cannot convert value of parameter 'ShippedDate' from 'System.String' to 'System.DateTime'.
Source Error:
Line 51: if (DetailsViewValidator.IsValid)
Line 52: {
Line 53: DetailsView1.UpdateItem(true);
Line 54: Label1.Text = "Hello";
Line 55: }
Source File: e:\My Local Documents\Visual Studio 2008\Websites\DD_NW\DynamicData\PageTemplates\Edit.aspx.cs Line:
53
Stack Trace:
[LinqDataSourceValidationException: Failed to set one or more properties on type Order. Cannot convert value of parameter 'ShippedDate' from 'System.String' to 'System.DateTime'.]
System.Web.UI.WebControls.LinqDataSourceView.BuildDataObject(Type dataObjectType, IDictionary inputParameters) +1009
System.Web.UI.WebControls.LinqDataSourceView.BuildUpdateDataObjects(Object table, IDictionary keys, IDictionary values, IDictionary oldValues) +379
System.Web.UI.WebControls.LinqDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +430
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92
System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation) +837
System.Web.UI.WebControls.DetailsView.UpdateItem(Boolean causesValidation) +36
Edit.Button1_Click(Object sender, EventArgs e) in e:\My Local Documents\Visual Studio 2008\Websites\DD_NW\DynamicData\PageTemplates\Edit.aspx.cs:53
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1546
This seems to be an issue with DateTime validation?
Dynamic DataValidation
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Am I right in thinking this method of posting back should work? This is a trivial example, but where this really comes into play is when I put dynamic fields in user controls and try to post back and validate them from the containing page. Works
great for everything but datetime fields.
Do you think I should post this back to the forum as a bug report, or will the team pick up on it here?
In the meantime, here's the workaround...
To DateTime_Edit.ascx I added a CustomValidator:
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="TextBox1" EnableClientScript="false"
onservervalidate="CustomValidator1_ServerValidate" ErrorMessage="must be a valid datetime value">Must be a valid datetime value</asp:CustomValidator>
When you call Page.Validate(), it evaluates a specific validation group, the empty string (""). I suspect that the validators within your DetailsView have a different validation group. As a result, Page.Validate() doesn't have any validators that report
an error (because there are no validators in its group) and sets Page.IsValid is true. Then you execute the update code which gets illegal data and fails.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Thanks for the help. I've opened a bug on this. Maira had the good idea to add a Required() attribute - If you enter an empty string and use the button, you correctly get an error saying the field is required. This seems to confirm the bug.
rmdaustin
Member
8 Points
19 Posts
How to submit and validate a dynamic data page programatically?
Jul 09, 2008 08:05 PM|LINK
I've run into a problem where a Dynamic Data page won't perform the server-side portion of the validation unless you submit via the controls in the details view. For example, I start with the Northwind database and a clean Dynamic Data project, create a Custom Page for the Orders table, and add a button below the DetailsView:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
In the code-behind, I add this:
protected void Button1_Click(object sender, EventArgs e)
{
DetailsViewValidator.Validate();
Page.Validate();
if (DetailsViewValidator.IsValid && Page.IsValid)
{
DetailsView1.UpdateItem(true);
}
}
Then I go ahead and put some bad date value (e.g. "aaa") in the Order Date field and try to submit using the Button (not the dynamic "Update") link.
If I do this in Firefox, nothing happens--the page submits, but nothing changes and the data is not updated.
In IE, similar, but I get a JavaScript error: "Sys.WebForms.PageRequestManager.ServerErrorException: Failed to set one or more properties on the type Order. Cannot convert value of paramter 'OrderDate' from 'System.String' to 'System.DateTime'" Weirdly, the Javascript error seems to come after the postback.
When I run this in the debugger, I get a "LinqDataSourceValidationException was unhandled by user code":
"Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.DateTime'."
In none of these cases does the validation error message show on the page, and both of the .IsValid tests are true.
All other kinds of client-side validation I've tried--length, required, Int--seem to work okay with the Button submit method.
If I submit using the DetailsView "Update" linkbutton, the date validation works as expected.
I'm running the 5-23a runtime and templates, VS2008SP1Beta1 and .NET Framework 3.5SP1 (Beta) on a Windows Server 2003 VM that has only been used for this Dynamic Data project.
Any ideas?
Thanks!
Richard
ASP.NET Dynamic Data Validation
scothu
Participant
1436 Points
291 Posts
Microsoft
Moderator
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 12:27 AM|LINK
Since you are getting a Javascript error the first thing you need to do is turn off the updatepanel logic so you can see the real error, this post will show you how to do that: http://blogs.msdn.com/davidebb/archive/2008/05/25/dynamic-data-ajax-and-javascript-errors.aspx.
PM, ASP.NET Team, Microsoft
rmdaustin
Member
8 Points
19 Posts
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 12:40 AM|LINK
I did try that. Then I just get the same error sent to the browser that I was already seeing in the debugger:
Server Error in '/Northwind4' Application.
--------------------------------------------------------------------------------
Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.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.Web.UI.WebControls.LinqDataSourceValidationException: Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.DateTime'.
Source Error:
Line 48: if (DetailsViewValidator.IsValid && Page.IsValid)
Line 49: {
Line 50: DetailsView1.UpdateItem(true);
Line 51: }
Line 52: }
Source File: c:\Inetpub\wwwroot\Northwind4\DynamicData\CustomPages\Orders\Edit.aspx.cs Line: 50
Stack Trace:
[LinqDataSourceValidationException: Failed to set one or more properties on type Order. Cannot convert value of parameter 'OrderDate' from 'System.String' to 'System.DateTime'.]
System.Web.UI.WebControls.LinqDataSourceView.BuildDataObject(Type dataObjectType, IDictionary inputParameters) +1003
System.Web.UI.WebControls.LinqDataSourceView.BuildUpdateDataObjects(Object table, IDictionary keys, IDictionary values, IDictionary oldValues) +383
System.Web.UI.WebControls.LinqDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +433
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92
System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation) +837
System.Web.UI.WebControls.DetailsView.UpdateItem(Boolean causesValidation) +36
Edit.Button1_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\Northwind4\DynamicData\CustomPages\Orders\Edit.aspx.cs:50
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1546
Richard
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 09:25 AM|LINK
Hi I've just replicated this issue, here is the code:
Added to the Edit.aspx page
Added to the code behind:
The results I get are:
Failed to set one or more properties on type Order. Cannot convert value of parameter 'ShippedDate' from 'System.String' to 'System.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.Web.UI.WebControls.LinqDataSourceValidationException: Failed to set one or more properties on type Order. Cannot convert value of parameter 'ShippedDate' from 'System.String' to 'System.DateTime'.
Source Error:
Source File: e:\My Local Documents\Visual Studio 2008\Websites\DD_NW\DynamicData\PageTemplates\Edit.aspx.cs Line: 53
Stack Trace:
This seems to be an issue with DateTime validation?
Dynamic Data Validation
Always seeking an elegant solution.
rmdaustin
Member
8 Points
19 Posts
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 04:20 PM|LINK
Thanks for verifying this Steve!
Am I right in thinking this method of posting back should work? This is a trivial example, but where this really comes into play is when I put dynamic fields in user controls and try to post back and validate them from the containing page. Works great for everything but datetime fields.
Do you think I should post this back to the forum as a bug report, or will the team pick up on it here?
In the meantime, here's the workaround...
To DateTime_Edit.ascx I added a CustomValidator:
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="TextBox1" EnableClientScript="false"
onservervalidate="CustomValidator1_ServerValidate" ErrorMessage="must be a valid datetime value">Must be a valid datetime value</asp:CustomValidator>
Then in DateTime_Edit.ascx.cs, this handler:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime dt;
args.IsValid = DateTime.TryParse(args.Value,out dt);
}
And to make the ValidationSummary make sense, added this in Page_Load:
if (!CustomValidator1.ErrorMessage.StartsWith(Column.DisplayName))
{
CustomValidator1.ErrorMessage = Column.DisplayName + " " + CustomValidator1.ErrorMessage;
}
Maybe not the elegant solution, but it works for now.
Richard
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 04:32 PM|LINK
I believe the team will find it and may offer other work arounds [:D]
Dynamic Data Validation
Always seeking an elegant solution.
PLBlum
All-Star
30399 Points
5347 Posts
MVP
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 05:36 PM|LINK
When you call Page.Validate(), it evaluates a specific validation group, the empty string (""). I suspect that the validators within your DetailsView have a different validation group. As a result, Page.Validate() doesn't have any validators that report an error (because there are no validators in its group) and sets Page.IsValid is true. Then you execute the update code which gets illegal data and fails.
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 06:00 PM|LINK
Sorry Peter but that's not it, I tried it with them all in the same group.
It works on everything if you click the DetailsView's Update link
And fails on date validation only on the button
[:(]
Dynamic Data Validation
Always seeking an elegant solution.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 08:11 PM|LINK
Thanks for the help. I've opened a bug on this. Maira had the good idea to add a Required() attribute - If you enter an empty string and use the button, you correctly get an error saying the field is required. This seems to confirm the bug.
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: How to submit and validate a dynamic data page programatically?
Jul 10, 2008 08:38 PM|LINK
Excelent I suspected as much, what I'm curious about is it an ASP.Net or a DynmaicData bug?
Dynamic Data Date Validation Issue
Always seeking an elegant solution.