New Columns added to Bound DataGrid not updating

Jun 18, 2009 at 2:48 AM

I am populating a dataGrid from a database.  The problem is that as the database will update properly with every edit using update.  But, when I add a column to the dataGrid in the code behind it will not update the database properly.  I can't design anything in xaml because the column rows change and vary.  It all has to be done in the code behind.

Coordinator
Jun 23, 2009 at 4:26 AM

What is triggering the update to the database?

Jun 23, 2009 at 4:01 PM

dataAdapter.Update(dataSet, dt.TableName);

Coordinator
Jun 23, 2009 at 9:14 PM

Could you supply a little more code so we can understand the context.  What do the bindings on the DataGridColumns look like?  What triggers the call to dataAdpator.Update() from the UI?

Jun 25, 2009 at 2:55 AM
Edited Jun 25, 2009 at 2:56 AM

This is just a test program using a button to test updating. No matter which way I add a column to the database the DataGrid will allow me to enter data but when clicking the button it does not update the database. Also you will notice that it is a sqlite database but it is ado and It hasn't given me any trouble yet.

Using

                    cmd = new SQLiteCommand("ALTER TABLE [" + tablename + "] ADD [" + field + "]" + fieldtype, sqliteconnection);
                    cmd.ExecuteNonQuery();
to add a column works if I call LoadTable again after but that's kind of a cludge fix isn't it?

Any Ideas??????

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            dataAdapter.Update(dataSet, "COURSE 1   05/01/1990");
        }
        private void LoadTable()
        {
            try
            {

                dataSet = new DataSet();
                string connString = String.Format("Data Source={0};New=False;Version=3", @"D:\icon\DEMO COMPANY\ANYTOWN USA\Pulp Mill\TANK-125\data.db");


                SQLiteConnection sqlconn = new SQLiteConnection(connString);
                sqlconn.Open();

                string CommandText = String.Format("Select * from [{0}];", "COURSE 1   05/01/1990");

                dataAdapter = new SQLiteDataAdapter(CommandText, sqlconn);
                SQLiteCommandBuilder builder = new SQLiteCommandBuilder(dataAdapter);
                this.dataAdapter.Fill(dataSet, "COURSE 1   05/01/1990");
                dataGrid1.ItemsSource = dataSet.Tables["COURSE 1   05/01/1990"].DefaultView;
            }
            catch (SQLiteException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

 

Jun 30, 2009 at 4:46 PM

bump

Jul 6, 2009 at 12:38 PM

Were there any ideas on this subject?

Coordinator
Jul 7, 2009 at 8:49 PM

It looks like it may be a problem with the SQL adapter.  Unfortunately we're not too familiar with the intricacies of SQL, so you may want to try posting this question on the MSDN SQL forums.