How can I pass a null value for date in vb.net to

2019-06-20 04:13发布

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.

2条回答
太酷不给撩
2楼-- · 2019-06-20 05:00
Dim pflt_dateto As SqlParameter = cmd.CreateParameter
        pflt_dateto.Direction = ParameterDirection.Input
        pflt_dateto.DbType = SqlDbType.Int
        pflt_dateto.IsNullable = True
        pflt_dateto.ParameterName = "flt_dateto"

        If String.IsNullOrEmpty(dateto) Then
            pflt_dateto.SqlValue = DBNull
        Else
            pflt_dateto.SqlValue = dateto
        End If

        cmd.Parameters.Add(pflt_dateto)

Since you set up the parameter nullable, the error will disappear. Now the issue is that .NET pass null integer as 0 so we need to deal with that.

Chao

查看更多
男人必须洒脱
3楼-- · 2019-06-20 05:08

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 
查看更多
登录 后发表回答