narasiman rao

narasiman rao

  • NA
  • 519
  • 771.9k

save the datagridview records into the database using

Mar 18 2013 6:35 AM

 how to save the datagridview items into the database using csharp
 
Note it is windows application.
 
In datagridview design screen as follows;
 
Name Mobile_No Message
 
A 9789503250 xxx
B 9840578905 yyy
C 9950412484 zzz
D 9790567890 aaa
E 9978904580 bbb
 
When i click the save button, the above datagridview records should be saved into the database.
 
 Save Button code as follows;

  private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                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 = " ";
                    GFun.InsertAccessData(sql);
                    if (GFun.Error.ToString() != "")
                    {
                        MessageBox.Show(GFun.Error.ToString(), "Error");
                        //this.Cursor = Cursors.Arrow;
                        return;
                    }
                    GFun.OleDbCon.Close();
                    MessageBox.Show("Record Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error", ex.ToString());
                return;
            }
        }


when i click the save button error shows as follows;

  syntax error in insert into  statement.

what is my problem, in my insert code.


please help me.


 


 

Answers (2)