0

I have first time created a table in Access it's name is punchMachineData it has following columns with their respective types given on the link http://prntscr.com/bjxs2v

i create a dynamic insert query like this :

  string str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sara\Desktop\punchMachineDataBase.accdb";
                    OleDbConnection conn = new OleDbConnection(str);
                    try
                    {
                        conn.Open();       
                        String my_querry = "INSERT into punchMachineData (empID,date,time,bstatus) Values('" + vSEnrollNumber + "','" + Convert.ToString(vYear) + "/" + String.Format("{0:D2}", vMonth) + "/" + String.Format("{0:D2}", vDay) + "','" + String.Format("{0:D2}", vHour) + ":" + String.Format("{0:D2}", vMinute) + "','" + bstatus + "')";

                        OleDbCommand cmd = new OleDbCommand(my_querry, conn);
                        cmd.ExecuteNonQuery();
                        Console.WriteLine("saved");                      
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Failed due to" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }

on debugging my_querry i get this : INSERT into punchMachineData (empID,date,time,bstatus) Values('1','2016/06/22','18:19','1')

and error i get in exception is : Syntax error in INSERT INTO statement

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement.
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()

Why i have it and How to fix it ?

5
  • Embrace your position like into [punchMachineData]... Source Commented Jun 23, 2016 at 9:54
  • 2
    Be careful with little Bobby Tables. Commented Jun 23, 2016 at 9:54
  • Beware of SQL Injection too Commented Jun 23, 2016 at 9:55
  • @SOURCE even if i do [punchMachineData] ..still smae errors Commented Jun 23, 2016 at 9:58
  • Schema columns are dates times not date time Commented Jun 23, 2016 at 10:29

2 Answers 2

1

The column names are different than the ones shared in the screenshot.

Use -

INSERT into punchMachineData (empID,dates,times,bstatus)...

Instead of -

INSERT into punchMachineData (empID,date,time,bstatus)...

Or change the name in schema instead.

On a side note, such commands are prone to SqlInjection so suggest to use parameterized queries in place of plain sql statements.

Sign up to request clarification or add additional context in comments.

2 Comments

parameterized queries means ?
@testtest - Sharing a nice link for understanding parameters - stackoverflow.com/questions/5893837/…
0

you need to encapsulate the table name and column name with square brackets

"INSERT into [punchMachineData] ([empID],[date],[time],[bstatus])......

A part from this, do not use string cancatenation to build sql commands. This practice leads to syntax error when in your input there is a single quote or do you have other fields that require a particular formatting of the input value. But the worst of all is the problem of Sql Injection

So your code should be written in this way:

INSERT into punchMachineData (empID,date,time,bstatus) Values
cmdInsert.CommandText = "INSERT INTO [punchMachineData ] (empID, date, time, bstatus) VALUES " + "(?,?,?,?)"
cmdInsert.Parameters.AddWithValue("@p1",pass param1 here)
cmdInsert.Parameters.AddWithValue("@p2",pass param2 here)
cmdInsert.Parameters.AddWithValue("@p3",pass param3 here)
cmdInsert.Parameters.AddWithValue("@p4",pass param4 here)
cmdInsert.Connection = cnnOLEDB
cmdInsert.ExecuteNonQuery() 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.