0

I thought this would be simpler but its giving me trouble. I basically want a user to check a few boxes and then add each individual checkbox to a row in a table. I tried a CheckBoxList but decided to just add each Checkbox individually. Basically this is my code:

  Using cn2 As New SqlConnection(connectionString)
        Dim cmd2 As New SqlCommand
        If chkActions.Checked = True Then
            cmd2 = New SqlCommand("Insert into [Turns] (Actions) VALUES ('1'", cn2)
            cn2.Open()
            cmd2.ExecuteNonQuery()
            cn2.Close()

        End If
    End Using

The Actions row is a bit datatype, so as far as I know its just a 1, 0 or NULL. It gives me an "Incorrect Syntax near..." error. If anyone can spot the error or maybe a better way of doing this I would be truly grateful.

4
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Mar 21, 2013 at 18:21
  • Thanks for the tip. Didn't know! Commented Mar 21, 2013 at 18:28
  • You add only if checkbox is checked? Commented Mar 21, 2013 at 18:29
  • Yes. Someone will check this box and it needs to add to the database as true. Commented Mar 21, 2013 at 18:36

3 Answers 3

1

you're treating your bit value as a string.

Remove the ' ' around 1. You also need a ) after the 1 to close the parentheses

I.E

cmd2 = New SqlCommand("Insert into [Turns] (Actions) VALUES (1)", cn2)
Sign up to request clarification or add additional context in comments.

4 Comments

I forgot I need to add a non null code to the DB, so I adjusted the code to this. cmd2 = New SqlCommand("Insert into [Turns2] (AcctNum), (Acciones) VALUES (@AcctNum, 1)", cn2) Its giving me a syntax error again.
It was the full syntax that was causing you issue. Wrong datatype and incorrect syntax.
Oh yeah I perfectly understood the change to (1), and I believe that worked perfectly. The only non-null column is the AcctNum one so I onl added the parameter for that one. The rest of the columns already have their information, I just basically need to "update" a process with a checked box. As far as I know I don't need to supply data to the rest of the columns because they can be left empty/they were already supplied information.
I see you have removed the ' in your new code :) if this answered your question please mark it as answered - or anybody elses for that matter. It helps us help you in the future :)
0

missing bracket at the end

cmd2 = New SqlCommand("Insert into [Turns] (Actions) VALUES ('1')", cn2)

1 Comment

This fixed one of the issues at least. I swear I tripled checked the code before asking. Semicolons, commas and parentheses are the end of me. Hah. Thanks!
0

Looks like you're missing a closing )

cmd2 = New SqlCommand("Insert into [Turns] (Actions) VALUES ('1'", cn2)

should probably be

cmd2 = New SqlCommand("Insert into [Turns] (Actions) VALUES ('1')", cn2)

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.