Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Where is TextBox1.Text defined? –  Joe Taylor Feb 20 '12 at 9:47
add comment

closed as off topic by Mokubai, slhck, DragonLord, iglvzx, Journeyman Geek Jul 19 '12 at 0:35

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

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!

share|improve this answer
    
It still says Incorrect syntax near 'mytextboxvalue'....what to do please help –  Legendary Lambe Feb 20 '12 at 9:25
    
what's the data type of FirstColumn? –  undone Feb 20 '12 at 9:26
    
VarChar (MAX) it is why? –  Legendary Lambe Feb 20 '12 at 9:30
    
answer please p –  Legendary Lambe Feb 20 '12 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 '12 at 9:46
show 4 more comments

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.

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

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