We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,348 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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.

2
Contributors
3
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
Axladrian
Newbie Poster
10 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Skill Endorsements: 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,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2964 seconds using 2.69MB