Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anybody give me an example for inserting data to database using String SQL command which includes insert into, values and also where?

I tried to write own-by-own but it gives me error which says incorrect syntax near the keyword where.

And this is my code;

C#
String SQL = " insert into measurrrecords(pulse)"+
             
             "values ('"+textBox1.Text"')"+

             "where ID= '"ComboBox1.SelectedIndex"'";



What do you think about my problem?
Posted
Updated 25-Jul-12 21:14pm
v2

You can't use a WHERE clause on an INSERT - and it makes no sense to :)

There are loads of SQL Tutorials[^] on the web, I'd suggest you read through some - you'll learn considerably more than getting piecemeal solutions on here
 
Share this answer
 
Comments
sesenyg 26-Jul-12 3:19am    
actually you are so right, now i understood the problem. It made me crazy for 2 days. Thank you so muchhh!!!!
I think two things:
1) You forgot a number of plus signs:
C#
"values ('"+textBox1.Text"')"+
Becomes
C#
"values ('"+textBox1.Text + "')"+
And
C#
"where ID= '"ComboBox1.SelectedIndex"'";
Becomes
C#
"where ID= '" + ComboBox1.SelectedIndex + "'";

and
2) You shouldn't do it like that. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
sesenyg 26-Jul-12 3:21am    
First of all thanks of your interest but i have already put + signs to my code. Moreover, barneyman answer my question. Thanks:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900