【发布时间】:2016-11-29 04:32:40
【问题描述】:
VS-Studio 2012 Web Express、ASP.NET、WebForms、VB、SqlServer、WebSite 应用程序无法将 DateTime 的 NULL 值保存到强类型 ROW:
Dim oRowVEHICLES As Main_TblAdap.tVEHICLESRow = odtVEHICLES.Rows(0) ' (0) is the first row.
oRowVEHICLES.[WElectrical] = If(WElectrical.Year < 10, DBNull.Value, WElectrical)
...etc...
目前,DetailsView 模板字段文本框为
DB 字段是 Date 类型,允许为空。
TableAdapter.xsd 视图显示默认值为
那么,为什么 oRowVehicles 的 Date 不能为空?
如何使 WElectrical 列 DATE 为空?
我一定忽略了一些东西,因为我不能是唯一一个将可选 DATE 值保存到 Sql-DB 的人。
欢迎您的 cmets 和解决方案。谢谢...约翰
编辑 详细信息视图中的 ASPX 代码一个 DATE 字段(其他类似):
<asp:TemplateField HeaderText="Electrical End Date" SortExpression="WElectrical">
<EditItemTemplate>
<TGADate:GADate ID="ucdtWElectrical" runat="server" Enabled="True" MinDate="01/01/1980" MaxDate="12/31/2050"
Caption="Electrical End Date" HideCaption="True" Width="100"
IsRequired="false"
UpdateMode="Conditional"
Text='<%# Bind("WElectrical")%>' />
</EditItemTemplate>
<InsertItemTemplate>
<TGADate:GADate ID="ucdtWElectrical2" runat="server" Enabled="True" MinDate="01/01/1980" MaxDate="12/31/2050"
Caption="Electrical End Date" HideCaption="True" Width="100"
IsRequired="false"
UpdateMode="Conditional"
Text='<%# Bind("WElectrical")%>' />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblWElectrical" runat="server" Text='<%# clsGA_Lib1.fnGetDateTextFromObject(Eval("WElectrical"))%>' Style="font-weight: bold;"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Bold="true" />
</asp:TemplateField>
ASPX 中的Object DataSource 参数定义。
<asp:Parameter Name="WElectrical" Type="DateTime" />
BLL 代码:
<System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, False)> _
Public Function UpdateFromDetailsView(ByVal original_UID_VEHICLE As Int32, _
ByVal VehicleNbr As String, _
...other function parameter variables...
ByVal WElectrical As Date, _
...other function parameter variables...
) As Boolean
' Get the new VEHICLE-row instance to be updated.
Dim odtVEHICLES As Main_TblAdap.tVEHICLESDataTable = Adapter.GetVhclByVhclID(original_UID_VEHICLE)
If odtVEHICLES.Count <> 1 Then
' no matching record found, return false
Return False
End If
' Populate the values of the ROW.
Dim oRowVEHICLES As Main_TblAdap.tVEHICLESRow = odtVEHICLES.Rows(0) ' (0) is the first row.
With oRowVEHICLES
...setting row-field values...
.[WElectrical] = If(WElectrical.Year < 10, Nothing, WElectrical)
...etc...
End With
' Update the oRowVEHICLES.
Dim rowsAffected As Integer = Adapter.Update(odtVEHICLES)
' Return TRUE if precisely one row was INSERTED, otherwise false.
Return CBool(rowsAffected = 1)
End Function
编辑上述代码的注释
进入 BLL 函数的 WElectrical 参数是一个 DATE,其值为 #01/01/0001#。
将值放入 ROW 对象的代码
.[WElectrical] = If(WElectrical.Year < 10, Nothing, WElectrical)
将 Nothing 作为行对象字段值。
Adapter.Update(odtVEHICLES) 更新 Sql-DB。
那么是什么导致 #01/01/0001# 值被放入 Sql-DB 中?
Sql-DB 列定义
//////// 编辑结束 ///////////
【问题讨论】:
-
如果您正在处理 .net 对象,请使用
null或Nothing。仅当您直接处理数据库时才使用DBNull,例如向查询或 SP 发送参数。WElectrical是DateTime?类型的字段吗?我不喜欢 VB,所以你可能需要按照这里的要求做:stackoverflow.com/questions/12595775/… -
感谢您的评论。该链接非常有用且非常重要。我将检查这些更改并让您知道。谢谢。
-
看起来您正在使用表格适配器。您需要将参数设置为 AllowNulls:msdn.microsoft.com/en-us/library/…
-
@JonP -- 谢谢,但数据集设计器已按照您的建议进行配置。
标签: asp.net sql-server datetime nullable bll