ASP.Net:插入null DateTime

时间:2010-05-23 14:44:30

标签: c# asp.net

我正在将SQLDatasource与Formview结合使用。 当我想插入或更新没有值的DateTime时,我总是会收到错误。

如何插入DateTime null。

我的SQLDataSource

<asp:SqlDataSource ID="sqldsDetailOrder" runat="server" ConnectionString="<%$ ConnectionStrings:csBookStore %>"
    SelectCommand="SELECT 'AUTHOR' = tblAuthors.FIRSTNAME + ' ' + tblAuthors.LASTNAME, tblBooks.*, tblGenres.*, tblLanguages.*, tblOrders.* FROM tblAuthors INNER JOIN tblBooks ON tblAuthors.AUTHOR_ID = tblBooks.AUTHOR_ID INNER JOIN tblGenres ON tblBooks.GENRE_ID = tblGenres.GENRE_ID INNER JOIN tblLanguages ON tblBooks.LANG_ID = tblLanguages.LANG_ID INNER JOIN tblOrders ON tblBooks.BOOK_ID = tblOrders.BOOK_ID WHERE tblOrders.ID = @ID" 
    DeleteCommand="DELETE FROM [tblOrders] WHERE [ID] = @ID" 
    InsertCommand="INSERT INTO [tblOrders] ([NAME], [ADDRESS], [CITY], [PC], [DATE], [BOOK_ID], [COUNT], [AMOUNT], [DELIVERED], [DDATE], [PAID], [PDATE]) VALUES (@NAME, @ADDRESS, @CITY, @PC, @DATE, @BOOK_ID, @COUNT, @AMOUNT, @DELIVERED, @DDATE, @PAID, @PDATE)" 
    UpdateCommand="UPDATE [tblOrders] SET [NAME] = @NAME, [ADDRESS] = @ADDRESS, [CITY] = @CITY, [PC] = @PC, [DATE] = @DATE, [BOOK_ID] = @BOOK_ID, [COUNT] = @COUNT, [AMOUNT] = @AMOUNT, [DELIVERED] = @DELIVERED, [DDATE] = @DDATE, [PAID] = @PAID, [PDATE] = @PDATE WHERE [ID] = @ID">
    <SelectParameters>
        <asp:ControlParameter ControlID="gvOrdersAdmin" Name="ID" 
            PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="NAME" Type="String" />
        <asp:Parameter Name="ADDRESS" Type="String" />
        <asp:Parameter Name="CITY" Type="String" />
        <asp:Parameter Name="PC" Type="String" />
        <asp:Parameter DbType="DateTime" Name="DATE" />
        <asp:Parameter Name="BOOK_ID" Type="Int32" />
        <asp:Parameter Name="COUNT" Type="Int32" />
        <asp:Parameter Name="AMOUNT" Type="Decimal" />
        <asp:Parameter Name="DELIVERED" Type="Boolean" />
        <asp:Parameter DbType="DateTime" Name="DDATE" />
        <asp:Parameter Name="PAID" Type="Boolean" />
        <asp:Parameter DbType="DateTime" Name="PDATE" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="NAME" Type="String" />
        <asp:Parameter Name="ADDRESS" Type="String" />
        <asp:Parameter Name="CITY" Type="String" />
        <asp:Parameter Name="PC" Type="String" />
        <asp:Parameter DbType="DateTime" Name="DATE" />
        <asp:Parameter Name="BOOK_ID" Type="Int32" />
        <asp:Parameter Name="COUNT" Type="Int32" />
        <asp:Parameter Name="AMOUNT" Type="Decimal" />
        <asp:Parameter Name="DELIVERED" Type="Boolean" />
        <asp:Parameter DbType="DateTime?" Name="DDATE" />
        <asp:Parameter Name="PAID" Type="Boolean" />
        <asp:Parameter DbType="DateTime" Name="PDATE" />
    </InsertParameters>
</asp:SqlDataSource>

感谢文森特

2 个答案:

答案 0 :(得分:1)

插入DBNull.Value,而不仅仅是null

答案 1 :(得分:0)

为了使用null DateTime,您需要包含Nullable。最常见的做法是将其声明为DateTime?(问号是使其可为空的语法)。