7

I am looking for the best practice, real solution, to send a Null to a SQL Server 2008 R2 database table, when a date is unknown.

I read some inputs from a formview, and a date field maybe unknown. The database allows Null values in the field but the VB to store the Null prior to a parameterized query update is not working/eludes me.

    Dim tb2 As TextBox = TryCast(FormView1.FindControl("tbPurchDate"), TextBox)
    Dim purDT As Date
    If tb2.Text = "" Then
        IsDBNull(purDT)    ' Tried this along with other possible code
    Else
        purDT = Convert.ToDateTime(tb2.Text)
    End If

Any help would be greatly appreciated.

2
  • If you mean this, I tried it....Dim purDT As Nullable(Of DateTime) Commented Mar 12, 2013 at 20:50
  • Oh, absolutely, I think I stated that in the OP as well. Commented Mar 12, 2013 at 20:55

4 Answers 4

13

If the date is unknown, send DbNull.Value as the parameter's value:

If dateIsUnknown Then
    cmd.Parameters.Add(New SqlParameter _
                       With {.ParameterName = "@purDT", _
                             .SqlDbType = SqlDbType.Date, _
                             .Value = DBNull.Value})
Else
    cmd.Parameters.Add(New SqlParameter _
                       With {.ParameterName = "@purDT", _
                             .SqlDbType = SqlDbType.Date, _
                             .Value = theDateVariable})
End If

Or if you prefer,

cmd.Parameters.Add(New SqlParameter With {
                   .ParameterName = "@purDT",
                   .SqlDbType = SqlDbType.Date,
                   .Value = If(dateIsUnknown, DBNull.Value, DirectCast(theDateVariable, Object))}
                  )

It is necessary to cast the variable to be of type Object so that the If operator has a common type to return. The Value parameter expects an Object.

Sign up to request clarification or add additional context in comments.

2 Comments

Mortan, Works, clean, formatting is outside my convention but I can live with that, and it works great. Thank you very much!!!
@marc11h You're welcome :) I wouldn't normally format it like that either, but it did fit into the available width.
1

It depends on the data method you are using to send the data to the server.

purDate is a variable of DateTime type and it cannot be set to null.

I suggest you use IsDate instead of testing length.

    Dim purDT As Date
    If Not IsDate(TextBox1.Text) Then
        purDT = Nothing
    Else
        purDT = Convert.ToDateTime(TextBox1.Text)
    End If

Comments

1

You could use the Nullable(Of Date) to allow your purDT variable to also be Nothing:

Dim purDT As Nullable(Of Date) = Nothing

If tb2.Text <> "" Then
    purDT = Convert.ToDateTime(tb2.Text)
End If

However, the magic happens when you define the SQL parameter that will hold this value. The parameter should either be DBNull.Value or a valid (non-null) Date:

' initialize connection, command...

Dim param = New SqlParameter()
param.ParameterName = "NullableDate"
param.Value = IIf(purDT Is Nothing, DBNull.Value, purDT)

cmd.Parameters.Add(param)
cmd.ExecuteNonQuery()

Comments

-1

Try this :

Dim purDT As Nullable(Of Date)

If tb2.Text = "" Then
    purDT = DBNull.Value
Else
    purDT = Convert.ToDateTime(tb2.Text)
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.