-1

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 列定义

在此处输入图像描述

//////// 编辑结束 ///////////

4

2 回答 2

0

做一件事:

将 DBNull.Value 更改为 Nothing

或者,您可以将数据集中的数据类型更改为 System.Object,然后转到该数据列的属性,然后您可以在下拉列表中选择“(无)”。

将 Null 值设置为 >> 无

于 2016-07-25T20:01:28.253 回答
0

感谢上述问题的所有评论员。解决方案是将 Row-column-variable 更改为将 Nothing to Date? (可为空)如下...

 .[WElectrical] = If(WElectrical.Year < 10, CType(Nothing, Date?), WElectrical)

和 -- 更改了数据集列定义(在 .xsd 中),如下所示:

DataType ==> System.Object(不是日期)

NullValue ==> (Nothing) (不抛出异常)

我衷心感谢所有贡献者——因为他们的每个建议元素都促成了这个解决方案。
这是将可空值发送到 DataRow 列的解决方案。

谢谢你的帮助。

于 2016-07-26T01:51:00.247 回答