I have databse in access 2007. my date datatype is Date/Time. I have made function to insert values to database.
But it is showingd syntax error(missing operation)..." for insering date.
Please help me how should i do i have tried hash in front of date and tried to save with
parameters.
My function is
public void purchaseProduct(string productname, string challan_no, DateTime challan_date, string bill_no, DateTime bill_date, string Customer)
{
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
OleDbCommand insert = new OleDbCommand("INSERT INTO [PRODUCT_DETAILS]([PRODUCT_NAME],[CHALLAN_NO],[CHALLAN_DATE],[BILL_NO],[BILL_DATE],[CUST_NAME])VALUES('"+productname+"','"+challan_no+"','#'+challan_date+'#','"+bill_no+"','#'+bill_date+'#','"+Customer+"')", con);
insert.ExecuteNonQuery();
MessageBox.Show("Your data has been saved.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
here how I am calling this function and I have used datetime picker.
add.purchaseProduct(txtproductname.Text.ToUpper(), txtchallan_no.Text.ToUpper(), txtchallan_date.Value.Date, txtbill_no.Text.ToUpper(), txtbill_date.Value.Date, txtCustomer.Text.ToUpper());
Rimmi90 0 Newbie Poster
Recommended Answers
Jump to PostOr use parameterized queries and you don't have to worry about formats.
Jump to PostPut at least one space/whitespace between keywords/identifier in SELECT statement and never use hardcoded string.
Have a look at : [CUST_NAME])VALUES('"+productname and INTO [PRODUCT_DETAILS]([PRODUCT_NAME]
Always use parameters.
using(OleDbConnection con=new OleDbConnection(cnStr)) { string sql="INSERT INTO [PRODUCT_DETAILS] ([PRODUCT_NAME],[CHALLAN_NO],[CHALLAN_DATE],[BILL_NO],[BILL_DATE],[CUST_NAME]) VALUES (@productName],@challanNo,@challanDate,@billNo,@billDate,@custName)"; using(OleDbCommand insert = new OleDbCommand(sql, con)) { insert.Parameters.AddWithValue("@productName",productname); //or …
All 5 Replies
ChrisHunter 152 Posting Whiz in Training Featured Poster
Momerath 1,327 Nearly a Senior Poster Featured Poster
Rimmi90 0 Newbie Poster
kvprajapati 1,826 Posting Genius Team Colleague
Rimmi90 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.