I have a test program consisting of a textbox (textbox1), a button (save), and a connection to a database and a table called TestDatabase.mdf and TestTable (respectively). I am trying to save the text which I will input into the database through 'textbox1'....

I have done this much so far:

Dim mytextboxvalue As String
    mytextboxvalue = TextBox1.Text
    sqCmd.CommandText = "INSERT INTO TestTable" & "(FirstColumn,   SecondColumn)" & "(mytextboxvalue,mytextboxvalue)"

And I am getting this run-time error:

Incorrect syntax near 'mytextboxvalue'.

So my question is that what is the correct syntax?

link|improve this question
Where is TextBox1.Text defined? – Joe Taylor Feb 20 at 9:47
feedback

2 Answers

up vote 0 down vote accepted
sqCmd.CommandText = "INSERT INTO TestTable" & "(FirstColumn,   SecondColumn) values" & "('mytextboxvalue','mytextboxvalue')"

And remember to use ' if they are string!

link|improve this answer
It still says Incorrect syntax near 'mytextboxvalue'....what to do please help – Legendary Lambe Feb 20 at 9:25
what's the data type of FirstColumn? – Death Feb 20 at 9:26
VarChar (MAX) it is why? – Legendary Lambe Feb 20 at 9:30
answer please p – Legendary Lambe Feb 20 at 9:33
It's beath to put an @symbol before the name of the person you are replying to. This will notify them. e.g @ Username – Joe Taylor Feb 20 at 9:46
show 4 more comments
feedback

I think you want

sqCmd.CommandText = "INSERT INTO TestTable (FirstColumn, SecondColumn) values ('" & mytextboxvalue & "','" & mytextboxvalue & "')"

... mytextboxvalue is a variable whose text you want to add into your INSERT INTO string.

Note that if anyone but yourself enters text into that textbox, you should not do it this way!! Have a look at the "Bobby Tables" issue for more details about avoiding SQL injections.

link|improve this answer
Thanks for the information – Legendary Lambe Feb 20 at 10:22
@Lambe: If my information is helpful, you should upvote the answer (click the upward arrow)... – Jonas Heidelberg Feb 20 at 13:49
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.