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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With