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 模板字段文本框为 <blank> 或空或“”,BLL 函数将其显示为日期,如:#01/01/0001#。因此,如果小于 10,我测试传入变量的 YEAR 值,然后将 DBNull.Value 保存到 oRowVehicles.[WElectrical] 但由于 datatype=Date 并且无法将 DBNull 转换为 Date 而失败。
DB 字段是 Date 类型,允许为空。
TableAdapter.xsd 视图显示默认值为 < DBNULL>。
那么,为什么 oRowVehicles 的 Date 不能为空?
如何使 WElectrical 列可以为空 DATE?
我一定忽略了一些东西,因为我不能是唯一一个将可选 DATE 值保存到 Sql-DB 的人。
欢迎您提出意见和解决方案。谢谢...约翰
在 DetailsView 中编辑 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 列定义
//////// 编辑结束 ///////////