Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have written this small piece of code and seem to be having all kinds of trouble with it! I know this is something simple I am just looking over but the error message I am getting is pretty much useless as I think my SQL statement is correct. If someone could have a look for me it would be appreciated.

private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection conn = new SqlConnection(Calorie_Counter_UK.Properties.Settings.Default.Calorie_CounterConnectionString);
            conn.Open();
            try
            {
                using (SqlCommand command = new SqlCommand("INSERT INTO User (Name,Age,Height,Weight,Weight_Target) VALUES (@Name,@Age,@Height,@Weight,@Target)", conn)) ;
                {
                    command.Parameters.Add(new SqlParameter("Name", txtName.Text));
                    command.Parameters.Add(new SqlParameter("Age", txtName.Text));
                    command.Parameters.Add(new SqlParameter("Height", txtName.Text));
                    command.Parameters.Add(new SqlParameter("Weight", txtName.Text));
                    command.Parameters.Add(new SqlParameter("target", txtName.Text));
                    Command.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("Could not Insert");
            }
        }
>

On command.ExecuteNonQuery(); I get the error - "Incorrect syntax near the keyword 'User'". Now I have looked and looked but to me the syntax for insert statement is correct. If anyone could advise it would be appreciated!

Thanks
Posted

Some of those table or column names are probably reserved words in your database engine. (User and Name I would be suspicious of.) You have to escape those in the query, either like `name` for mySQL or [name] for SQL server (if I'm remembering that right).
 
Share this answer
 
Comments
DanHodgson88 1-Aug-11 6:00am    
Thanks guys its was a combination of all 3! 5* appreciate your time :)
try to find out in the following way

INSERT INTO [User] (Name,Age,Height,Weight,Weight_Target) VALUES (@Name,@Age,@Height,@Weight,@Target)
 
Share this answer
 
Comments
DanHodgson88 1-Aug-11 6:00am    
Thanks guys its was a combination of all 3! 5* appreciate your time :)

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