Question

I have a date field (smalldatetime) in a table and in particular cases I need to set the date field to null. Currently, when setting the sqlparameter.value property to a date

endDateParam.Value = "5/15/2011"

the field is updated with the proper date. However, setting it to

endDateParam.Value = System.DbNull.Value

doesn't update the field.

Code behind:

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

Update

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

Still doesn't seem to update the field. No errors are returned.

Was it helpful?

Solution

Use SqlDateTime.Null instead.

OTHER TIPS

Try SqlDateTime.Null instead of system.dbnull.value

I would recommend that you use Sql Profiler to see exactly what is getting sent to the server. That should help you narrow down the problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top