-1

VS-Studio 2012 Web Express、ASP.NET、WebForms、VB、SqlServer、WebSite アプリケーションで、厳密に型指定された ROW に DateTime の NULL 値を保存する際に問題が発生しました。

      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 テンプレート フィールドのテキスト ボックスは < 空白> または空または "" であり、BLL 関数はそれを #01/01/0001# のような日付として表示します。したがって、渡された変数の YEAR 値が 10 未満の場合はテストし、DBNull.Value を oRowVehicles.[WElectrical] に保存しますが、datatype=Date であるため失敗し、DBNull を Date に変換できません。

DB フィールドは日付型で、NULL を許可します。

TableAdapter.xsd ビューは、デフォルト値が < DBNULL> であることを示しています。

では、なぜ oRowVehicles が Date nullable でないのでしょうか?

WElectrical 列を NULL 可能な DATE にするにはどうすればよいですか?

オプションのDATE値をSql-DBに保存するのは私だけではないので、何かを見落としているに違いありません。

あなたのコメントと解決策は大歓迎です。ありがとう...ジョン

DetailsView の 1 つの DATE フィールドを ASPX コードで編集します (他も同様です)。

              <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 のオブジェクト 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 パラメーターは、値が #01/01/0001# の DATE です。
値を ROW オブジェクトに配置するコード

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

row-object-field-value として Nothing を配置します。

Adapter.Update(odtVEHICLES)は、 Sql -DB を更新します。

では、#01/01/0001# の値が Sql-DB に配置される原因は何でしょうか?

SQL-DB 列の定義

ここに画像の説明を入力

//////// 編集終了 ///////////

4

2 に答える 2

0

1 つのことを行います。

DBNull.Value を Nothing に変更します

または、データセットのデータ型を System.Object に変更し、そのデータ列のプロパティに移動して、ドロップダウンで [(Nothing)] を選択することもできます。

Null 値を >> Nothing に設定します

于 2016-07-25T20:01:28.253 に答える