VS工作室2012网络快车,ASP.NET,WebForms的,VB,SQLSERVER,有麻烦的DateTime NULL值保存到强类型ROW网页应用:如何将空日期时间保存到SqlServer数据库?不工作
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#。所以我测试传入变量的YEAR值,如果小于10,则将DBNull.Value保存到oRowVehicles [WElectrical],但由于datatype = Date而失败,并且无法将DBNull转换为Date。
DB字段是Date类型并允许空值。
TableAdapter.xsd视图显示默认值为< DBNULL>。
那么,为什么oRowVehicles不能日期为空?
如何使WElectrical列可以为空?
我必须忽略一些东西,因为我不能将唯一一个将可选DATE值保存到Sql-DB。
欢迎您提出意见和解决方案。谢谢...约翰
编辑 ASPX代码DATE一个在DetailsView(其它类似)域:在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>
DataSource对象的参数定义。
<asp:Parameter Name="WElectrical" Type="DateTime" />
BLL代码:对于上面的代码
的WElectrical参数进入该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
编辑评论是用#01 /的值的DATE 01/0001#。
的代码值放入ROW对象
.[WElectrical] = If(WElectrical.Year < 10, Nothing, WElectrical)
地方没有什么作为行对象的字段值。
Adapter.Update(odtVEHICLES)更新Sql-DB。
那么是什么导致#01/01/0001#值被放入Sql-DB?
SQL的数据库列定义
////////编辑结束///////////
如果您正在处理.net对象,请使用'null'或'Nothing'。只有在直接处理数据库时才使用'DBNull',比如向查询或SP发送参数。 'WElectrical'是'DateTime'类型的字段吗?我没有进入VB,所以你可能需要做的是在这里问:http://stackoverflow.com/questions/12595775/how-do-i-set-a-nullable-datetime-to-null-in-vb -net – Andrew
谢谢你的评论。该链接非常丰富,非常重要。我将检查这些更改并通知您。谢谢。 –
看起来你正在使用表格适配器。 YOu需要将该参数设置为AllowNulls:https://msdn.microsoft.com/en-us/library/ms233762.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 –