错误:当我选择日期时,从字符串转换日期和/或时间时转换失败?

时间:2013-05-07 03:37:39

标签: c# asp.net sqldatasource

我有这个gridview,我使用sqldatasource

这是我的选择命令,

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:TAXIConnectionString %>" 
    SelectCommand="SELECT [TripId] FROM [BookingMaster] 
                    WHERE(([PickupLocation] = @PickupLocation or @PickupLocation is null) 
                    AND ([DestinationLocation] = @DestinationLocation 
                    or @DestinationLocation is null ) 
                    AND ([Status] = @Status or @Status is null) 
                    AND (cast([PickupDateTime] as date) = @PickupDateTime 
                    or @PickupDateTime is null))" 
     CancelSelectOnNullParameter="False">
         <SelectParameters>
             <asp:ControlParameter ControlID="ddPickup" Name="PickupLocation" 
                  PropertyName="SelectedValue" Type="String" 
                  ConvertEmptyStringToNull="true" />
            <asp:ControlParameter ControlID="ddDestination" Name="DestinationLocation" 
                 PropertyName="SelectedValue" Type="String" 
                 ConvertEmptyStringToNull="true" />
            <asp:ControlParameter ControlID="ddStatus" Name="Status" 
                 PropertyName="SelectedValue" Type="String" 
                 ConvertEmptyStringToNull="true" />
            <asp:ControlParameter ControlID="txtBookingDate" Name="PickupDateTime" 
                 PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" />
        </SelectParameters>
    </asp:SqlDataSource>    

但错误发生在我选择13之后的日期或者......

Server Error in '/' Application.

Conversion failed when converting date and/or time from character string.

这有什么不对吗?

1 个答案:

答案 0 :(得分:2)

对我来说听起来像是一个本地化问题。我猜你要用dd / mm / yy格式输入日期,但是ASP.NET或SQL Server试图用不变文化或en-US来解析它们,这需要mm / dd / yy格式。或者相反。将日期传递给SQL Server时,最好使用DateTime.Parse解析它们,或者使用yyyy-MM-dd HH:mm:ss格式将它们作为字符串传递,这完全是明确的。