Skip to content

Unable to update database from datagridview

Smita

Asked Aug 11, 2010, 12:58 PM EDT1 answer
Share
LinkedInX
Hi ,

I am trying to update database through datagridview, its neither throwing any error nor updating the database.

SQLiteConnection con = new SQLiteConnection(@"data source=C:\SQLiteDB\Test.sqlite");
        SQLiteCommand Cmd = new SQLiteCommand() ;
        SQLiteDataAdapter ad = new SQLiteDataAdapter();
        DataSet ds = new DataSet();
        DataRow dr;
        DataTable dt = new DataTable();
        SQLiteCommandBuilder bd;
        SQLiteCommandBuilder cb;

form_load
{

 con.Open();
                    SQLiteCommand Cmd = con.CreateCommand();
                    Cmd.CommandText = "select * from SqliteSmithaTbl";
                    SQLiteDataAdapter ad = new SQLiteDataAdapter(Cmd.CommandText, con);

                    ad.Fill(ds, "test");
                    //ad.Fill(dt);

                    cb = new SQLiteCommandBuilder(ad);
                    BindingSource bSource = new BindingSource();


                    bSource.DataSource = ds.Tables["test"];
                    dataGridView1.DataSource = bSource;
                    


}

  button click event       
{
cb.RefreshSchema();
           dataGridView1.EndEdit();
           ad.AcceptChangesDuringUpdate = true;
           ds.AcceptChanges();
 
           ad.UpdateCommand = cb.GetUpdateCommand();
           ad.InsertCommand = cb.GetInsertCommand();
           //ad.DeleteCommand = cb.GetDeleteCommand();

           ad.Update(ds.Tables["test"]);
           ad.Dispose();
           con.Close();

}

Please help..........!!

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Got it resolved.Had to remove  the following two lines of code.
    Had to let the CommandBuilder take care of it.....

    ad.AcceptChangesDuringUpdate = true;
    ds.AcceptChanges();

    --Smitha
    0