c# 4.0 - How to insert data from datetimepicker to ms access using c#? -


i beginners using inserting data datetimepicker ms access. code is:

    private void savesale()     {         string connstring = configurationmanager.connectionstrings["saledbx"].connectionstring;         string cmdstring = "insert sale (date, sale, expense) values (@date, @sale, @expense)";          using(oledbconnection con = new oledbconnection(connstring))         {             using(oledbcommand cmd = new oledbcommand(cmdstring, con))             {                 con.open();                  cmd.parameters.addwithvalue("@date", todaydatepicker.text);                 cmd.parameters.addwithvalue("@sale", saletextbox.text);                 cmd.parameters.addwithvalue("@expense", expensetextbox.text);                  cmd.executenonquery();                  messagebox.show("today sale added successfully!", "sale saved", messageboxbuttons.ok, messageboxicon.information);             }         }     } 

i facing error:

an unhandled exception of type 'system.data.oledb.oledbexception' occurred in system.data.dll

additional information: syntax error in insert statement.

date reserved keyword. if can't change field's name, should enclose between square brackets

string cmdstring = "insert sale ([date], sale, expense) values (@date, @sale, @expense)"; 

next, suggest use add instead of addwithvalue in particular because working dates prone many conversion errors.

cmd.parameters.add("@date", oledbtype.date).value = todaydatepicker.value; 

in way can tell database engine exact datatype of parameter passed , avoid conversion string datetime values


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -