Search code examples
asp.netsql-serverdatetimenullablebll

How to save a null datetime to SqlServer DB? not working


VS-Studio 2012 Web Express, ASP.NET, WebForms , VB , SqlServer , WebSite application having trouble saving a NULL value for DateTime to the strongly typed 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...

Currently the DetailsView template field textbox is < blank> or empty or "" and the BLL function shows it as a date like: #01/01/0001#. So I test the YEAR value of the passed in variable if less than 10 then save DBNull.Value to the oRowVehicles.[WElectrical] but fails since datatype=Date and cannot convert DBNull to Date.

The DB-field is type Date and allows nulls.

The TableAdapter.xsd view shows the default value is < DBNULL>.

So, why is the oRowVehicles not Date nullable?

How do I make the WElectrical column nullable DATE?

I must be overlooking something, because I cannot be the only one to save an optional DATE value to the Sql-DB.

Your comments and solutions are welcome. Thanks...John

EDIT ASPX code one DATE field in the DetailsView (others are similar):

              <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>

Object DataSource parameter definition in the ASPX.

 <asp:Parameter Name="WElectrical" Type="DateTime" />

BLL Code:

   <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

Edit comment for above code

The WElectrical parameter coming into the BLL-function is a DATE with a value of #01/01/0001#.
The code to place the value into the ROW-object

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

places Nothing as the row-object-field-value.

The Adapter.Update(odtVEHICLES) updates the Sql-DB.

So what is causing the #01/01/0001# value to be placed into the Sql-DB?

Sql-DB column definition

enter image description here

//////// end of Edit ///////////


Solution

  • Thanks to all commentators to the above question. The solution is to change the Row-column-variable to this sentence which casts the Nothing to Date? (nullable) as follows...

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

    AND -- Changed the dataset-column-definition (in the .xsd) as follows:

    DataType ==> System.Object (not Date)

    NullValue ==> (Nothing) (not Throw Exception)

    My sincere thanks to all contributors -- since elements of each of their suggestions have contributed to this solution.
    This is the solution for sending a nullable value into the DataRow-column.

    Thank you for all your help.