Click here to Skip to main content
15,888,055 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
it is giving me a syntax error
C#
con.Open();
OleDbCommand cmd =
    new OleDbCommand(
        "insert into coupen(voucher,RS,Date) values('" + _voucher + "','" + TxtAmount.Text + "',GetDate())", con);
int result = cmd.ExecuteNonQuery();
con.Close();
Posted
Updated 19-Mar-13 22:48pm
v3
Comments
Shubh Agrahari 20-Mar-13 4:07am    
what is data type of date column in db...?
Amirsalgar1 20-Mar-13 4:42am    
date and time
Amirsalgar1 20-Mar-13 4:45am    
in my access database in table coupon there are three columns voucher, Rs,date

here voucher, Rs are working properly , i want to insert current date and time while clicking

Try this:
C#
con.Open();
OleDbCommand cmd =
    new OleDbCommand(
        "insert into coupen(voucher,RS,[Date]) values('" + _password + "','" + TxtAmount.Text + "',"+ DateTime.Now.Date + ")", con);
int result = cmd.ExecuteNonQuery();
con.Close();
 
Share this answer
 
v2
Comments
Amirsalgar1 20-Mar-13 4:41am    
nahh dude it still giving me a same error

Syntax error in INSERT INTO statement.
Amirsalgar1 20-Mar-13 4:46am    
in my access database in table coupon there are three columns voucher, Rs,date here voucher, Rs are working properly , i want to insert current date and time while clicking
date and time is data type of date column in db
Kuthuparakkal 20-Mar-13 5:22am    
Try now, it could be the column name match with DataType name, so enclose those columns in [] brackets. Modified solution too...
Amirsalgar1 21-Mar-13 4:36am    
will you please join me via team viewer ? my ID 175 735 453 pwd 4281
getdate() is the sql function it will not execute using oledb. And, one more point is you cannot send getdate() from codebehind using ado.net.

change the query,


C#
con.Open();
OleDbCommand cmd =
    new OleDbCommand(
        "insert into coupen([voucher],[RS],[Date]) values('" + _password + "','" + TxtAmount.Text + "','"+ System.DateTime.Now + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();


try this.
if error occurs paste the error occuring.
 
Share this answer
 
Comments
Amirsalgar1 21-Mar-13 4:35am    
will you please join me vie team viewer ? my ID 175 735 453 pwd 4281
Try this:
C#
con.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO COUPEN(voucher,RS,Date) VALUES ('" + _password + "','" + txt.Text + "','" + DateTime.Now + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();


Try to crosscheck your table/column name once.

--Amit
 
Share this answer
 
v2
It is a really bad habit to build your SQL queries by concatenating string fields like you do.
Always do it with parameterized queries.
Thus :

con.Open();
OleDbCommand cmd = new OleDbCommand("insert into coupen(voucher,RS,Date) values(@voucher, @amount, @now)", con);
cmd.Parameters.AddWithValue("voucher", _voucher);
cmd.Parameters.AddWithValue("amount", TxtAmount.Text);
cmd.Parameters.AddWithValue("now", DateTime.Now);
int result = cmd.ExecuteNonQuery();
con.Close();


This way your code won't be prone to SQL injection attacks, and it's a little bit readable.

Hope this helps.
 
Share this answer
 
Comments
Zukiari 20-Mar-13 6:01am    
Hi, Try by assigning the date time value to a variable.
DateTime dt = System.DateTime.Now;
con.Open();
OleDbCommand cmd =
new OleDbCommand(
"insert into coupen(voucher,RS,[Date]) values('" + _password + "','" + TxtAmount.Text + "',"+ dt + ")", con);
int result = cmd.ExecuteNonQuery();
con.Close();
Amirsalgar1 21-Mar-13 4:33am    
will you please join me vie team viewer ?
my ID 175 735 453
pwd 4281

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900