Click here to Skip to main content
13,141,576 members (56,473 online)
Rate this:
Please Sign up or sign in to vote.
Hi everyone!

I am having an issue with dataadapter update methods. Here is what I have so far:

List<string> tempColumnList = new List<string>();
                List<string> tempColumnFinalList = new List<string>();
                SqlCeCommand getTableListingCommand = new SqlCeCommand("SELECT table_name FROM information_schema.tables " + 
                    "WHERE table_name <> 'engineandtransmissioncodes' AND table_name <> 'enginecodemaster' AND table_name <> 'transmissioncodemaster' AND table_name <> 'vehicles' AND table_name <> 'waveform_files'", conn);
                getTableListingCommand.CommandType = CommandType.Text;
                SqlCeDataReader getTableListingCommand_dr = getTableListingCommand.ExecuteReader();
                DataTable dt_temp = new DataTable();
                while (getTableListingCommand_dr.Read())
                    BindingSource bstemp = new BindingSource();
                    SqlCeDataAdapter da_temp = new SqlCeDataAdapter("SELECT * FROM " + getTableListingCommand_dr.GetString(0), conn);
                    SqlCeCommandBuilder scb_temp = new SqlCeCommandBuilder(da_temp);
                    SqlCeCommand getColumnListingCommand = new SqlCeCommand("SELECT column_name FROM information_schema.columns WHERE table_name = @table_name", conn);
                    getColumnListingCommand.CommandType = CommandType.Text;
                    getColumnListingCommand.Parameters.AddWithValue("@table_name", getTableListingCommand_dr.GetString(0));
                    SqlCeDataReader getColumnListingCommand_dr = getColumnListingCommand.ExecuteReader();
                    while (getColumnListingCommand_dr.Read())
                        //MessageBox.Show(getTableListingCommand_dr.GetString(0) + " " + getColumnListingCommand_dr.GetString(0));
                    foreach (string s in tempColumnList)
                        if (dataGridView1.Columns.Contains(s))
                            tempColumnFinalList.Add(dataGridView1.Rows[dataGridView1.NewRowIndex - 1].Cells[s].Value.ToString());
                        else //if(dataGridView1.Columns.Contains(s) == false)
                            tempColumnFinalList.Add(dataGridView1.Rows[dataGridView1.NewRowIndex - 1].Cells["KEY"].Value.ToString());
                    string[] tempColumnFinalListArray = tempColumnFinalList.ToArray();                   
                    //dataGridView1.DataSource = dt_temp;
                    //bstemp.DataSource = dt_temp;

The expected output would be to add the row contained in the tempColumnFinalListArray to the dt_temp datatable to each of the tables in my datatable (with non-null *correct* values).

The actual output is it updates the first table in my database correctly and then updates the rest of the tables that succeed it with rows that are null (other than the "key" value.

The gettablelistingcommand returns all table names. The getColumnListingCommand returns all columns in all tables.

The foreach(string s in tempColumnList) is supposed to add each value of the new row to the database. The reason for the if/else statement is because some tables have "KEY" and some have "VEHICLE_KEY". I didn't know how to differentiate from that. It is supposed to add it to a list and then convert it to an array to be added to the respective table via a datarow.

Can anyone shed some light for me as to what I'm missing so the tables can get updated properly?
Posted 8-Mar-13 11:58am
Updated 8-Mar-13 12:00pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100