4

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

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I've search in the net but I got no answer. What I did is I've used a blank image (totally white image) stored as default picture if no picture will be saved. Hoping this will help.

Comments

0

I solved it this way to make set with NULL value.

 If yourVariable Is Nothing Then
    comm.Parameters.AddWithValue("@detalleDesc", DBNull.Value)
Else
    comm.Parameters.AddWithValue("@detalleDesc", yourVariable )
End If

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.