Click here to Skip to main content
15,886,771 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my project I want to insert data to a table from another table in the database

I used query as follows


C#
String st = "select code from state where name='" + this.statename.Text  + "'";
                            String str = "insert into branch(Code,Name,metro,state)values ('" + this.txtbranchcode.Text + "','" + this.txtbranchname.Text + "','" + this.cmbmetro.SelectedIndex + "','" + st + "')";


but compile time no error
at run time it is showing error as syntax error at Karnataka

here Karnataka means my statename.text

pls correct me error
Posted
Updated 13-Mar-15 3:31am
v3

I agree with the previous answer that this is a very dangerous approach. If the user is allowed to enter a value into `this.statename.Text` they might type
'; DROP DATABASE; --
The quote closes the open quote making it an empty string, the semicolon terminates the statement and SQL engine then processes the DROP DATABASE as a fresh statement. Ouch!
The `--` following the DROP DATABASE makes the rest of the line a comment. Check out this site: http://michaeldaw.org/sql-injection-cheat-sheet

Anyway...

I think where you're going wrong syntactically is that you are putting single quotes around your select statement in the insert statement, thereby turning it from a select statement into a string literal. I.e.:
values( ..., 'select code from state where name='Karnataka'')
Furthermore, since it already has single quotes in it, it becomes syntactically correct.

My SQL is a bit rusty but I don't think you can have select statements inside VALUES(). I think they have to be literals.
Try this instead. It uses a SELECT to insert into the table:

SQL
String str = "INSERT INTO branch(Code,Name,metro,state)"
+ " SELECT '" + this.txtbranchcode.Text + "','" 
+ this.txtbranchname.Text + "','" 
+ this.cmbmetro.SelectedIndex + "'," 
+ " code FROM state WHERE name='" + this.statename.Text + "'";


Better would be to validate the statename (that it has only letters and no quotes or semicolons etc). Better still would be to send it as a parameterised query - this avoids all the so called SQL injection attacks.
 
Share this answer
 
v2
select code from state where name='Karnataka'

It is kind of nasty that people are entering a state as free text and you're looking up the code that goes with it. You should use the debugger to get the SQL string, then run it in SQL Server to get more detailed errors and/or confirm that the SQL is right. You may need to put things like name in [], because they may be reserved words.

I still think your second line is going to break. Either that, or your database design is really crap. Just like the rest of your code ( I see you still don't care about how secure this code is, or about any sort of basic principles of software design )

I've done some testing, and the SQL I typed in above, which is what should be generated, will work fine on a table called state, with fields called code and name. Perhaps you need to post the scheme of your DB ( by editing your post ). It's clear you have no idea what you're doing, so it's very hard to work out what you're doing wrong exactly, it could be anything.
 
Share this answer
 
v2

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