Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I have to execute various sql queries but my table name is stored in a variable.
I am having problem in inserting data code of INSERT INTO command shows syntax error:

C#
using (OleDbCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "INSERT INTO" + inp_table + ""+ "([date],[open],[high],[low],[close],[volume])" + "values(@date, @open,@high,@low,@close,@volume)";

                 String dt = dateTimePicker1.Value.ToShortDateString();
                cmd.Parameters.AddRange(new OleDbParameter[]
                {
                    new OleDbParameter("@date",dt),
                    new OleDbParameter("@open",textBox1.Text ),
                    new OleDbParameter("@high",textBox2.Text ),
                    new OleDbParameter("@low",textBox3.Text ),
                    new OleDbParameter("@close",textBox4.Text),
                    new OleDbParameter("@volume",textBox5.Text)
                });
                cmd.ExecuteNonQuery();
            }


Please help asap
Posted
Updated 28-Mar-12 13:03pm
v2

1 solution

I think you may find you need to add a couple of spaces and reformat the string:

C#
cmd.CommandText = "insert into " + inp_table + " ([date],[open],[high],[low],[close],[volume]) values(@date, @open,@high,@low,@close,@volume)";


I won't mention stored procedures. :-)
 
Share this answer
 
Comments
Member 8722300 28-Mar-12 19:22pm    
Thank you very much. I can't believe it was only formatting problem that is bothering me from 2 days.
thanks a ton.
RaisKazi 28-Mar-12 19:28pm    
My 5.

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