954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem inserting into database

I got the following error in my code today:
Syntax error in INSERT INTO statement.

OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText="Insert into Cursanti([ID],[Nume],[Prenume],[Sex],[Varsta],[Ziua-1],[Ziua-2],[Ora-1],[Ora2],[E-mail],[Telefon],[Referal],[Partic/Grup],[Adult/Copil)] Values ('"+id_b.Text+"','"+name_t.Text+"','"+surname_t.Text+"','"+sex+"',"+age_t.Text+"','"+ziua1.Text+"','"+ziua2.Text+"','"+ora1.Text+"','"+ora2.Text+"','"+email_b.Text+"','"+phone_b.Text+"','"+from_t.Text+"','"+ore+"','"+categorie+"')";
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();


The error points at "cmd.ExecuteNonQuery();"

Searched the other topics, saw that people had similar problems, but i didn't quite get what should be done. It's my first day working with databases as well so sorry if it's a silly question.

Things i did to try and solve the problem:
1. Added [] for each ID, Nume etc.
2. Checked if the types are all the same.
3. Checked for anything typed wrong.

Sorry for some of the code, but used romanian in my project.

Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Oh and yea, if i just add a dateTimePicker to the form that i'm using, is it ok if i use the same example as above to store the date? like "+dateTimePicker1.Value+".

Tell me if you need any other info.

Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Looks like you're missing a closing parenthesis right before the word VALUES
...or that it's inside the ] bracket instead of outside.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Dooh.. thx, that solved it...
As well, had a few more errors. And in the end it looks like this:

OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText="Insert into Cursanti([ID],[Nume],[Prenume],[Sex],[Varsta],[Ziua-1],[Ziua-2],[Ora-1],[Ora-2],[E-mail],[Telefon],[Referal],[Partic/Grup],[Adult/Copil]) Values ('"+id_b.Text+"','"+name_t.Text+"','"+surname_t.Text+"','"+sex_t.Text+"','"+age_t.Text+"','"+ziua1.Text+"','"+ziua2.Text+"','"+ora1.Text+"','"+ora2.Text+"','"+email_b.Text+"','"+phone_b.Text+"','"+from_t.Text+"','"+ore+"','"+categorie+"')";
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();


I'm happy now :D Thanks!

Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: