0

I want to insert a checkbox value into a SQL Server database but it does not work. May I know how to insert that value into a SQL Server database?

This is my code:

Dim serUpdate As string

Try sConn.Open
    If checkbox.Checked = True Then
       serUpdate = "INSERT INTO xxxx(checkbox) VALUES ('Y')"
End If

sCmd.CommandText = serUpdate
sCmd.ExecuteNonQuery()

Catch ex As Exception
    Response.write(ex)

Finally

    sConn.Close()

End Try
21
  • 2
    What does "not work" mean? Does it error? Does it give unexpected behaviour? Becomes sentient? Help us help you. Commented May 14, 2021 at 8:19
  • Boolean values are represented by an integer usually where 0 = false and <> 0 true Commented May 14, 2021 at 8:20
  • 2
    I would, personally, use a bit in SQL Server for a boolean, @G3nt_M3caj . Commented May 14, 2021 at 8:22
  • @Larnu SQL Server BIT data type is an integer data type that can take a value of 0, 1, or NULL isn't it? :) Commented May 14, 2021 at 8:25
  • 2
    No, @G3nt_M3caj , it is not. It does not behave the same as a int. Commented May 14, 2021 at 8:27

1 Answer 1

3
Using connection As New SqlConnection("connection string here"),
      command As New SqlCommand("INSERT INTO MyTable (MyBitColumn) VALUES (@MyBitColumn)", connection)
    command.Parameters.Add("@MyBitColumn", SqlDbType.Bit).Value = myCheckBox.Checked
    connection.Open()
    command.ExecuteNonQuery()
End Using

Done!

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

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.