asp.net - 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 null value datetime typed row:

      dim orowvehicles main_tbladap.tvehiclesrow = odtvehicles.rows(0)  ' (0) first row.       orowvehicles.[welectrical] = if(welectrical.year < 10, dbnull.value, welectrical) ...etc... 

currently detailsview template field textbox < blank> or empty or "" , bll function shows date like: #01/01/0001#. test year value of passed in variable if less 10 save dbnull.value orowvehicles.[welectrical] fails since datatype=date , cannot convert dbnull date.

the db-field type date , allows nulls.

the tableadapter.xsd view shows default value < dbnull>.

so, why orowvehicles not date nullable?

how make welectrical column nullable date?

i must overlooking something, because cannot 1 save optional date value sql-db.

your comments , solutions welcome. thanks...john

edit aspx code 1 date field in detailsview (others 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 aspx.

 <asp:parameter name="welectrical" type="datetime" /> 

bll code:

   <system.componentmodel.dataobjectmethodattribute(system.componentmodel.dataobjectmethodtype.update, false)> _ public function updatefromdetailsview(byval original_uid_vehicle int32, _                                      byval vehiclenbr string, _ ...other function parameter variables...                                            byval welectrical date, _ ...other function parameter variables...                                          ) boolean    ' new vehicle-row instance updated.   dim odtvehicles main_tbladap.tvehiclesdatatable = adapter.getvhclbyvhclid(original_uid_vehicle)    if odtvehicles.count <> 1      ' no matching record found, return false      return false   end if    ' populate values of row.   dim orowvehicles main_tbladap.tvehiclesrow = odtvehicles.rows(0)  ' (0) first row.   orowvehicles      ...setting row-field values...      .[welectrical] = if(welectrical.year < 10, nothing, welectrical)      ...etc...   end    ' update orowvehicles.   dim rowsaffected integer = adapter.update(odtvehicles)    ' return true if precisely 1 row inserted, otherwise false.   return cbool(rowsaffected = 1) end function 

edit comment above code

the welectrical parameter coming bll-function date value of #01/01/0001#.
code place value row-object

.[welectrical] = if(welectrical.year < 10, nothing, welectrical) 

places nothing row-object-field-value.

the adapter.update(odtvehicles) updates sql-db.

so causing #01/01/0001# value placed sql-db?

sql-db column definition

enter image description here

//////// end of edit ///////////

do 1 thing:

change dbnull.value nothing

alternatively can change datatype in dataset system.object, , go properties of data colm can select '(nothing)' in dropdown.

set null value >> nothing


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -