1,105,288 Community Members

Problem inserting into database

Member Avatar
Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
1
 

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

Member Avatar
Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Question Answered as of 2 Years Ago by thines01
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: