Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using vbNull to insert NULL into a sql server database table

I need to insert text MALE or FEMALE into a table depending upon the user's input.

I used to following code to insert but the code inserts value 1 if none (Male/Female) selected.

query = "INSERT INTO student_profile_table(gender) VALUES(@gender)"
cmd = New SqlCommand(query, con)

If rbtmale.Checked = True Then
   cmd.Parameters.AddWithValue("@gender", "Male")
ElseIf rbtfemale.Checked = True Then
   cmd.Parameters.AddWithValue("@gender", "Female")
Else
   cmd.Parameters.AddWithValue("@gender", vbNull)
End If

con.Open()
cmd.ExecuteNonQuery()
con.Close()

Need some suggestions/corrections

like image 576
Random User Avatar asked Feb 16 '23 14:02

Random User


1 Answers

I believe you want System.DbNull.Value instead of vbNull.

System.DbNull.Value is the constant that SQL Server understands as null.

vbNull is intended to indicate if a Variant type is null.

like image 119
Brad Rem Avatar answered Feb 18 '23 18:02

Brad Rem