Using vbNull to insert NULL into a SQL Server database table

I need to insert the text MALE or FEMALE into the table depending on user input.

I used the following code to insert, but the code inserted a value of 1 if none were selected (Male / Female) .

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

+4
source share
2 answers

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

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

vbNull intended to indicate whether the Variant type is null.

+7
source

I am searching online, but I have no answer. What I did, I used a blank image (completely white image), saved as the default image if the image was not saved. Hope this helps.

0
source

Source: https://habr.com/ru/post/1496600/


All Articles