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...
****************** -
//opens database
-
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
-
+ "Data Source=C:\\TimeClock\\TimeClock.mdb";
-
-
OleDbConnection empConnection = new OleDbConnection(conString);
-
-
//add time clocked in/out in clock table
-
string insertStatement = "INSERT INTO Clock "
-
+ "(Login, Time, Status) "
-
+ "VALUES (?, ?, ?)";
-
-
OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);
-
-
insertCommand.Parameters.Add("Login", OleDbType.Char).Value = strLogin;
-
insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
-
insertCommand.Parameters.Add("Status", OleDbType.Char).Value = strStatus;
-
-
empConnection.Open();
-
-
try
-
{
-
int count = insertCommand.ExecuteNonQuery();
-
}
-
catch (OleDbException ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
finally
-
{
-
empConnection.Close();
-
}
*****************
11 106415
You must write -
string insertStatement = "INSERT INTO Clock "
-
+ "(Login, Time, Status) "
-
+ "VALUES (@Login, @Time, @Status)";
-
-
insertCommand.Parameters.Add("@Login", OleDbType.Char).Value = strLogin;
-
insertCommand.Parameters.Add("@Time", OleDbType.Char).Value = strTime;
-
insertCommand.Parameters.Add("@Status", OleDbType.Char).Value = strStatus;
I think its right
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.
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.
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
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.
try -
string insertStatement = "INSERT INTO Clock "
-
+ "([Login], [Time], [Status]) "
-
+ "VALUES (@Login, @Time, @Status)";
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.
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
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
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)";
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |