How can I pass a null value for date in vb.net to sql stored procedure?

54,929

Just assign the parameter the value DBNull.Value

[EDIT]

If you are using SqlParameter (thanks Jon) then you can set it like this if it gives any error

parameter.Value = DBNull.Value 
Share:
54,929
Nandini
Author by

Nandini

Updated on May 03, 2020

Comments

  • Nandini
    Nandini about 4 years

    My application is asp.net with vb.

    In my page I have a textbox for passing date. If I didn't enter date and on clicking submit, I have to pass null value to the stored procedure.

    I tried following codes such as DBNull.Value and DateTime.MinValue. In that case instead of null, "#12:00:00#" is passing. I have to pass Null.

  • Nandini
    Nandini over 15 years
    it gives a conversion problem.can it be solved by Convert.ToDateTime(DBNull.Value)
  • Nandini
    Nandini over 15 years
    DBNull.Value cannot be used as it generates an error "Object cannot be cast from DBNull.Value to other types"
  • Jon Skeet
    Jon Skeet over 15 years
    The code given should be fine, if "parameter" is a SqlParameter.
  • AllisonC
    AllisonC about 7 years
    Shouldn't this line be pflt_dateto.SqlValue = DBNull.Value ? I got the error: "DBNull is a type and cannot be used as an expression" without .Value