Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 正在使用空值更新列_Vb.net_Sql Server 2008_Sqldatasource_Dbnull - Fatal编程技术网

Vb.net 正在使用空值更新列

Vb.net 正在使用空值更新列,vb.net,sql-server-2008,sqldatasource,dbnull,Vb.net,Sql Server 2008,Sqldatasource,Dbnull,我在表中有一个日期字段(smalldatetime),在特定情况下,我需要将日期字段设置为null。当前,将sqlparameter.value属性设置为日期时 endDateParam.Value=“5/15/2011” 该字段将更新为正确的日期。但是,将其设置为 endDateParam.Value=System.DbNull.Value 不更新字段 代码隐藏: Protected Sub ... For Each r As GridViewRow In gvEmployees.Ro

我在表中有一个日期字段(smalldatetime),在特定情况下,我需要将日期字段设置为null。当前,将sqlparameter.value属性设置为日期时

endDateParam.Value=“5/15/2011”

该字段将更新为正确的日期。但是,将其设置为

endDateParam.Value=System.DbNull.Value

不更新字段

代码隐藏:

Protected Sub ...
    For Each r As GridViewRow In gvEmployees.Rows
        SqlDataSource1.UpdateCommand = "<stored proc>"
        SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure
        setParameters(r)
        gvEmployees.UpdateRow(r.RowIndex, False)
    Next
End Sub

Private updateParameters As New List(Of SqlParameter)()

Protected Sub setParameters(ByVal r As GridViewRow)
    updateParameters.Clear()

    Dim endDate As TextBox = TryCast(r.FindControl("txtEndDate"), TextBox)

    Dim endDateParam As New SqlParameter("@enddate", SqlDbType.SmallDateTime)
    endDateParam.Direction = ParameterDirection.Input
    endDateParam.Value = System.DBNull.Value
    updateParameters.Add(endDateParam)
End Sub

Protected Sub Sqldatasource1_Updating(ByVal source As Object, ByVal e As SqlDataSourceCommandEventArgs)
    e.Command.Parameters.Clear()
    For Each p As SqlParameter In updateParameters
        e.Command.Parameters.Add(p)
    Next
End Sub
受保护的子系统。。。
对于gvEmployees.Rows中的每个r作为GridViewRow
SqlDataSource1.UpdateCommand=“”
SqlDataSource1.UpdateCommand类型=SqlDataSourceCommandType.StoredProcess
设置参数(r)
gvEmployees.UpdateRow(r.RowIndex,False)
下一个
端接头
私有updateParameters作为(SqlParameter的)新列表()
受保护的子集合参数(ByVal r作为GridViewRow)
updateParameters.Clear()
Dim endDate As TextBox=TryCast(r.FindControl(“txtEndDate”),文本框)
Dim endDateParam作为新的SqlParameter(“@enddate”,SqlDbType.SmallDateTime)
endDateParam.Direction=ParameterDirection.Input
endDateParam.Value=System.DBNull.Value
updateParameters.Add(endDateParam)
端接头
受保护的子Sqldatasource1_更新(ByVal源作为对象,ByVal e作为SqlDataSourceCommandEventArgs)
e、 Command.Parameters.Clear()
对于updateParameters中的每个作为SqlParameter的p
e、 Command.Parameters.Add(p)
下一个
端接头
更新

endDateParam.Value=System.Data.SqlTypes.SqlDateTime.Null


似乎仍然没有更新字段。没有返回错误。

请尝试使用SqlDateTime.Null而不是system.dbnull.value。

请改用。

我建议您使用Sql Profiler查看发送到服务器的确切内容。这将有助于缩小问题的范围

发生了什么?错误?例外情况?什么都没有?只是一个想法,当存储过程看到一个空参数时,它是否有可能执行某些操作?表中的endDate字段中出现了什么?@Sparky-你是对的。在该存储过程中,我们执行了另外两个存储过程,其中一个在查看后基于空值退出。