asigning null to vb.net variable

  • My requirement is to assign a null value to vb.net variable which is declared as DIM st1 as String and pass this varible to stored procdure along other 20+ variables. I tried with assigning "nothing" to variable but it is inserting spacess rather than null and also tried DBNULL.Value which gives me error not able to convert to string.

     

     

    Is there any way I can achive desired results

  • You need to check the value of the variable and assign System.DBNull.Value to the sql parameter value, not the variable. e.g.

    Dim customerName As String = String.Empty

    Dim prm As New System.Data.SqlClient.SqlParameter("@name", SqlDbType.VarChar, 50);

    If customerName = String.Empty OrElse customerName Is Nothing

    prm.Value = System.DBNull.Value

    Else

    prm.Value = customerName

    End If

    Developers usually create a specific class for converting to and from null values when reading resultsets and setting parameters.

    Hope this helps

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply