ObjectDataSource – Cannot convert value of parameter value from System.String to System.DateTime us culture

Category ASP.Net Sub Category Database Interaction
Bookmark and Share
When doing the update the ObjectDataSource always uses the en-US culture and not the culture defined for the application.
Add these fixes below to pass us date in.


Protected Sub odsAddEdit_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles odsAddEdit.Updating

Copy Code
Protected Sub odsAddEdit_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles odsAddEdit.Updating For Each de As DictionaryEntry In e.InputParameters Dim currentDate As DateTime = DateTime.MinValue If de.Value IsNot Nothing AndAlso DateTime.TryParse(de.Value.ToString(), currentDate) Then e.InputParameters(de.Key) = currentDate.ToString(New Globalization.CultureInfo("en-US")) End If Next End Sub Protected Sub odsAddEdit_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles odsAddEdit.Inserting For Each de As DictionaryEntry In e.InputParameters Dim currentDate As DateTime = DateTime.MinValue If de.Value IsNot Nothing AndAlso DateTime.TryParse(de.Value.ToString(), currentDate) Then e.InputParameters(de.Key) = currentDate.ToString(New Globalization.CultureInfo("en-US")) End If Next End Sub


NOTE for vwd 2010 it does not allow you to change the source of the for each so the following approach had to be taken:

Copy Code
Protected Sub odsAddEdit_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles odsAddEdit.Updating For i = 0 To e.InputParameters.Count - 1 Dim currentDate As DateTime = DateTime.MinValue If e.InputParameters(i) IsNot Nothing AndAlso DateTime.TryParse(e.InputParameters(i).ToString(), currentDate) Then e.InputParameters(i) = currentDate.ToString(New Globalization.CultureInfo("en-US")) End If Next 'For Each de As DictionaryEntry In e.InputParameters ' Dim currentDate As DateTime = DateTime.MinValue ' If de.Value IsNot Nothing AndAlso DateTime.TryParse(de.Value.ToString(), currentDate) Then ' e.InputParameters(de.Key) = currentDate.ToString(New Globalization.CultureInfo("en-US")) ' End If 'Next End Sub Protected Sub odsAddEdit_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles odsAddEdit.Inserting For i = 0 To e.InputParameters.Count - 1 Dim currentDate As DateTime = DateTime.MinValue If e.InputParameters(i) IsNot Nothing AndAlso DateTime.TryParse(e.InputParameters(i).ToString(), currentDate) Then e.InputParameters(i) = currentDate.ToString(New Globalization.CultureInfo("en-US")) End If Next 'For Each de As DictionaryEntry In e.InputParameters ' Dim currentDate As DateTime = DateTime.MinValue ' If de.Value IsNot Nothing AndAlso DateTime.TryParse(de.Value.ToString(), currentDate) Then ' e.InputParameters(de.Key) = currentDate.ToString(New Globalization.CultureInfo("en-US")) ' End If 'Next End Sub

Share the love
Bookmark and Share