Click here to Skip to main content
15,936,988 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried several times but it shows the is not working.

Save Code as follows.
for (int i = 0; i < DGV_Fac_SMS.RowCount; i++)
      sql = "insert into Tb_Faculty_Schedule_Timetable [Name],[Mobile No],[Message];
      sql = sql + " values ( '" + (DGV_Fac_SMS.Rows[i].Cells[0].Value.ToString()) + "','" + (DGV_Fac_SMS.Rows[i].Cells[1].Value.ToString()) + "','" + (DGV_Fac_SMS.Rows[i].Cells[2].Value.ToString()) + "')";

      GFun.Error = " ";
      if (GFun.Error.ToString() != "")
             MessageBox.Show(GFun.Error.ToString(), "Error");

      MessageBox.Show("Record Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);

When I click the save button, error shows as follows.

syntax error in  insert into sql statement.

What is the mistake in insert query ?
I tried several times, but it shows the error.
Rohit Kumar Mumbai 18-Mar-13 9:51am    
Can you please debug this code and check data in sql variable before calling InserAccessData, and provide the same here?
[no name] 18-Mar-13 9:54am    
Probably because your sql string is not terminated and you did not put "()" around your column names. I do not see how you would get an SQL syntax error since this code will not compile. And I still cannot believe that after all this time and repeatedly telling you to use parameterized queries, you still insist on using string concatenation. Not looking to pass this class are you?
Jegan Thiyagesan 18-Mar-13 9:57am    
This is member only cares about finishing off his homework, he doesn't give a toss to learning about coding!
[no name] 18-Mar-13 10:14am    
Nope.... he is only trying to get people to do his work for him one vague, or easily answered, question at a time.
joshrduncan2012 18-Mar-13 10:15am    
I think the () are missing around the column names.

1 solution

You have done...
sql = "insert into Tb_Faculty_Schedule_Timetable [Name],[Mobile No],[Message];

sql = sql + " values ( '" + (DGV_Fac_SMS.Rows[i].Cells[0].Value.ToString()) + "','" + (DGV_Fac_SMS.Rows[i].Cells[1].Value.ToString()) + "','" + (DGV_Fac_SMS.Rows[i].Cells[2].Value.ToString()) + "')";

-> In first line of code, you missed a quote at last before semicolon as said by @Jegan Thiyagesan.

So, it should become...
sql = "insert into Tb_Faculty_Schedule_Timetable [Name],[Mobile No],[Message]";

sql = sql + " values ( '" + (DGV_Fac_SMS.Rows[i].Cells[0].Value.ToString()) + "', '" + (DGV_Fac_SMS.Rows[i].Cells[1].Value.ToString()) + "','" + (DGV_Fac_SMS.Rows[i].Cells[2].Value.ToString()) + "')";

Correct and let us know it helped or not.
Share this answer

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