364,085 Members | 5432 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

C# INSERT statement using OleDb

themoonisdown09
P: 11
I am using Visual Studio 2003, C# .NET

I can't get this INSERT statement to work. I can get an UPDATE to work, but not this INSERT. Here is my code...

******************
Expand|Select|Wrap|Line Numbers
  1. //opens database
  2. string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
  3.             + "Data Source=C:\\TimeClock\\TimeClock.mdb";
  4.  
  5. OleDbConnection empConnection = new OleDbConnection(conString);
  6.  
  7. //add time clocked in/out in clock table
  8. string insertStatement = "INSERT INTO Clock "
  9.                      + "(Login, Time, Status) "
  10.                      + "VALUES (?, ?, ?)";
  11.  
  12. OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);
  13.  
  14. insertCommand.Parameters.Add("Login", OleDbType.Char).Value = strLogin;
  15. insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
  16. insertCommand.Parameters.Add("Status", OleDbType.Char).Value = strStatus;
  17.  
  18. empConnection.Open();
  19.  
  20. try
  21. {
  22. int count = insertCommand.ExecuteNonQuery();
  23. }
  24. catch (OleDbException ex)
  25. {
  26. MessageBox.Show(ex.Message);
  27. }
  28. finally
  29. {
  30. empConnection.Close();
  31. }
*****************
Jul 26 '06 #1
Share this Question
Share on Google+
11 Replies


axas
P: 32
You must write
Expand|Select|Wrap|Line Numbers
  1. string insertStatement = "INSERT INTO Clock "
  2. + "(Login, Time, Status) "
  3. + "VALUES (@Login, @Time, @Status)";
  4.  
  5. insertCommand.Parameters.Add("@Login", OleDbType.Char).Value = strLogin;
  6. insertCommand.Parameters.Add("@Time", OleDbType.Char).Value = strTime;
  7. insertCommand.Parameters.Add("@Status", OleDbType.Char).Value = strStatus;
I think its right
Jul 27 '06 #2

themoonisdown09
P: 11
It's still saying that there is a syntax error in the INSERT INTO statement. I don't understand. I have a book that shows me how to do it... and I tried AXAS code... and it still says that.
Jul 27 '06 #3

Enyi
P: 38
I tried making a program with your code, and i got the same error.

Here is your problem:

The word 'Time' is a keyword in MS Access 2003, and most likely other versions as well. Therefore, it cannot be used as a column name.

I havn't tried with a different column name for time, but i'm sure it would work.

Good luck!


EDIT: Actually, I'm just thinking. How can update work in this situation? Now I'm questioning myself. I'll just try the program again.
Jul 28 '06 #4

Enyi
P: 38
OK, I just tried changing the column name and it worked just fine with both an UPDATE and INSERT INTO command.

However, when I tried with 'Time' as the column name in an UPDATE statement, it just said "Syntax error in UPDATE statement".

Not sure how you got update to work, but I can't :P ( With a column named 'Time' )

Of course, I'm using MS Access 2003, maybe your verison is different?

Enyi
Jul 28 '06 #5

themoonisdown09
P: 11
I didn't use an UPDATE with this exact statement. It was with another part of my program. I'll try and change the column name when I get home... I'm at work now... ha. Thanks so much for your help... I'll let you know if it works for me too.

By the way... I am using Microsoft Access 2003.
Jul 28 '06 #6

Elena
P: 3
try
Expand|Select|Wrap|Line Numbers
  1. string insertStatement = "INSERT INTO Clock "
  2. + "([Login], [Time], [Status]) "
  3. + "VALUES (@Login, @Time, @Status)";
Jul 28 '06 #7

themoonisdown09
P: 11
THANK YOU ELENA!!! I changed the column name and I also put the brackets around the names.... don't know which thing helped, but it works now.
Jul 28 '06 #8

Enyi
P: 38
Very nice Elena.

I seen what it does it now. The square brackets explicitly refers to the column name, whereas without square brackets, it could be referring to a column or function or something else. Right?

I would also say in our cases, both helped :D
Jul 29 '06 #9

dna5122
P: 12
You are correct Anyi.
Jul 29 '06 #10

TracyM
P: 3
I came across this same problem with Access and Visual Studio. Visual Studio does not support read and writes in its Adapter to Oledb. Its not bi-directional. Just read only. I know its a major pain. Its the last thing I expected also. I suppose it forces you to use Sqlserver. There are other sources out there for MySql and some other databases but you may not have time.

You have a couple choices here. If available switch to SqlServer DB or try another method involving manipulation of Access:

Take a look at this link and see if it helps.

http://www.geekpedia.com/tutorial158_Connect-to-Access-Database-in-Visual-Studio-.NET.html

Best of Luck,
TracyM
Jul 31 '06 #11

gts2277
P: 1
string insertStatement = "INSERT INTO Clock "
+ "([Login], [Time], [Status]) "
+ "VALUES (@Login, @Time, @Status)";

with the brack is WORK !!!!! and save your time use @ infront of VALUES. Like:

string insertStatement = "INSERT INTO Clock "
+ "([Login], [Time], [Status]) "
+ @"VALUES Login,Time,Status)";
Apr 15 '07 #12

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework c# insert oledb insert oledb insertid