473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# INSERT statement using OleDb

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
11 106415
axas
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
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
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
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
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
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
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
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
12
You are correct Anyi.
Jul 29 '06 #10
TracyM
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
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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mvr | last post by:
Hi all I are using IIS 5.0, Oracle 8.1. I am having problem with the following Insert Statement when used on Production Web server with SSL(https://...., Verisign). This doesn't occur...
4
by: NS | last post by:
Hi, I am trying to execute a prepare statement using oledb provider for DB2. The command.Prepare() statement is giving me an exception " No error information available:...
4
by: Robert Hanson | last post by:
Hi All, I am trying to add a record to a datatable that is connected to an Access database. I had no trouble with string and date fields, but for this record, I have two Long Integer field...
2
by: Paul Mason | last post by:
Hi folks, The ado.net stream appears to be not working so I'm here. The following function generates the error "Operation must use an updateable query". There is no identifiable or meaningful...
11
by: sm | last post by:
Hi All, Can anybody give me the syntax to insert a record into SQL server through VB code using variables? The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2,...
7
by: Cindy H | last post by:
Hi I'm having a problem getting the insert statement correct for an Access table I'm using. The Access table uses an autonumber for the primary key. I have tried this: INSERT INTO Tournaments...
2
by: jayjayplane | last post by:
I want to insert value into one temp table, but based on the value from another table, like: select s2_sess_datetime,s2_individual_session from lop_2008_staging if s2_sess_datetime is not null...
2
by: mnarewec | last post by:
I would like to execute multiple SQL Statement using OleDB command but its coming up with error " Characters found after end of statement" Below is my pieces of code. Public Class DalOleDb...
2
by: vasanth chandrasekaran | last post by:
Hai All, I try to insert the Dataset value in Exiting excel file by using OLEDB provider.But i face error messagge in objCmd.ExecuteNonQuery(); The error is :Syntax error in INSERT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.