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

本文关键字:转换 日期 时间 失败 字符串 错误 选择 | 更新日期: 2023-09-27 18:12:19

我有这个gridview,我使用sqldatasource

这是我的select命令,

<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.

这有什么问题?

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

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