-1

Here is my code and I am getting insert error.

Kindly help

OleDbConnection cnn = new OleDbConnection(dbConnection);

cnn.Open();

OleDbCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;

cmd.CommandText = "INSERT INTO [Emp Data] (" +
"EmpID, Active, EmpName, DOJ, DOL, [Next Manager], DOB, Department, Section, Designation, [Father Name], Address, Town, CNIC, Education, [Habib Metro], [Salary PM], EmailID, [JS Bank Account], [Salary on joining], [Last inc Rs], [Last inc Date], [Next of Kin Name], Relation, [Contact No], Comments, [Reason of Leaving], DOC, [Shift Timings], [Off Day 1], [Off Day 2]"
+ ") VALUES (" +
id + ", 'A' , '" + name + "', '" + doj + "', null '" + manager + "', '" + dob + "', '" + dept + "', '" + section + "', '" + desg + "', '" + father + "', '" + add + "', '" + town + "', '" + cnic + "', '" + education + "', '" + metroBank + "', " + salaryPM + ", '" + email + "', '" + jsBank + "', " + salary + ", 0, 0, null, '" + kinName + "', '" + kinRelation + "', '" + kinContact + "', '" + comments + "', null '" + doc + "', '" + shift + "', '" + offDay1 + "', '" + offDay2
+ "');";
cmd.ExecuteNonQuery();

cnn.Close();
Rizwan Ahmed
  • 399
  • 4
  • 8
  • Pro tip, remove the `
    ` tags in your post, then highlight all the code and press `CTRL+K`
    – Bender Bending Aug 02 '17 at 21:30
  • Good, that we all are seers and we all know,, what kind of error you are getting. But wait... we are not.... So please state one. – Piotr Aug 02 '17 at 21:31
  • its throwing exception {"Syntax error in INSERT INTO statement."} – Rizwan Ahmed Aug 02 '17 at 21:36
  • With just a glance I can just about guarantee you are making a mistake with concatenating all those variables. Either you have improper quoting or nulls, or both, etc. Use parameterized queries to alleviate these issues and also prevent SQL injection. – Crowcoder Aug 02 '17 at 21:36

1 Answers1

0

I suspect:

null '"

should be:

null, '"

You are missing a comma in a couple of places.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • after adding comma it is still not working the query is working but when I am sending query through c# code it throwing error of Syxtax error in INSERT INTO statement – Rizwan Ahmed Aug 03 '17 at 00:20
  • Just showing that Syxtax error in INSERT INTO statement – Rizwan Ahmed Aug 03 '17 at 18:23
  • No it isn't @RizwanAhmed . That is the `exception`. I want to know the contents of `cmd.CommandText`. Put a breakpoint on the `cmd.ExecuteNonQuery();` line and tell us what the value of `cmd.CommandText` is. https://blogs.msdn.microsoft.com/devops/2016/07/15/7-ways-to-look-at-the-values-of-variables-while-debugging-in-visual-studio/ – mjwills Aug 03 '17 at 21:58
  • the value on execute non query method The name 'ExecuteNonQuery' does not exist in the current context – Rizwan Ahmed Aug 03 '17 at 23:33
  • @RizWanAhmed There is absolutely nothing I can do to help if you can't share the contents of `cmd.CommandText`. – mjwills Aug 03 '17 at 23:47