0

I have a problem when I want to save the data in the database. the exception is:

System.InvalidOperationException: 'DataColumn' Name_Faculty 'missing in DataTable' disconnGrid 'for SourceColumn' Name_Faculty '.'

public void fillGrid()
        {
            if (adoF2.ds.Tables["disconnGrid"] != null)
            {
                adoF2.ds.Tables["disconnGrid"].Clear(); // to get new update.
            }
                adoF2.da = new SqlDataAdapter("SELECT * FROM Students", adoF2.con1);
                adoF2.da.Fill(adoF2.ds, "disconnGrid");
                dataGridView1.DataSource = adoF2.ds.Tables["disconnGrid"];
        }
public void fillCombo()
        {
            adoF2.da = new SqlDataAdapter("SELECT * FROM Faculty", adoF2.con1);
            adoF2.da.Fill(adoF2.ds, "disconnCombo");
            comboBox1.DataSource = adoF2.ds.Tables["disconnCombo"];
            comboBox1.DisplayMember = adoF2.ds.Tables["disconnCombo"].Columns[1].ColumnName;
            comboBox1.ValueMember = adoF2.ds.Tables["disconnCombo"].Columns[0].ColumnName; /* this column is a foreign key into 'Students' table*/
        }
private void gestionStagiaire2_Load(object sender, EventArgs e)
        {
            fillGrid();
            fillCombo();
        }
// to add the new row in the dataTable[disconnGrid]
private void button1_Click(object sender, EventArgs e)
        {
            adoF2.row1 = adoF2.ds.Tables["disconnGrid"].NewRow();
            adoF2.row1[0] = textID.Text;
            adoF2.row1[1] = textName.Text;
            adoF2.row1[2] = comboBox1.SelectedValue; // to get the value not what is diplayed on the UI of comboBox1.
            for (int i = 0; i < adoF2.ds.Tables["disconnGrid"].Rows.Count; i++)
            {
                if (textID.Text == adoF2.ds.Tables["disconnGrid"].Rows[i].ToString())
                {
                    MessageBox.Show("Sorry the students is existed", "Warning", MessageBoxButtons.OK);
                    return; // to avoid errors.
                }
            }
            adoF2.ds.Tables["disconnGrid"].Rows.Add(adoF2.row1);
            MessageBox.Show("The students has been added successfully", "info", MessageBoxButtons.OK,MessageBoxIcon.Information);
            dataGridView1.DataSource = adoF2.ds.Tables["disconnGrid"]; // to get the newly update.
        }
// to save the data into the database:
private void saveB_Click(object sender, EventArgs e)
        {
            adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da);
            adoF2.da.Update(adoF2.ds.Tables["disconnGrid"]);
            MessageBox.Show("Saving has been succeeded", "Info", MessageBoxButtons.OK);
        }
Thom A
  • 88,727
  • 11
  • 45
  • 75
  • Not much info to go on here. Why would it be asking for `Name_Faculty` column, where is it declared? Aside: [I hope you are closing and disposing your connection](https://stackoverflow.com/questions/17552829/c-sharp-data-connections-best-practice) – Charlieface Feb 10 '21 at 14:24
  • `Name_Faculty` is a column in the table `Faculty` – Soufiane Feb 10 '21 at 15:11
  • Clearly it got confused between the two tables. And I suppose it begs the question why you are caching the adapter and reusing it, at the very least have a different one for each table. This line `adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da);` doesn't seem to be doing anything, not sure why it's there, looks like you might have wanted to change the adapter's Command property instead – Charlieface Feb 10 '21 at 15:32
  • `adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da);` is used to generate automatically the INSERT query to make the `adoF2.da` SqlDataAdapter obejct adds new row into the database. – Soufiane Feb 10 '21 at 17:31
  • I just don't see the code for that. I can't see the declaration of `adoF2` so don't know if it has any code to do that, but I'm guessing it doesn't and that's your issue. – Charlieface Feb 10 '21 at 17:33
  • Yes, I think that the code is correct but when I run the code I have this exception: System.InvalidOperationException: 'DataColumn' Name_Faculty 'missing in DataTable' disconnGrid 'for SourceColumn' Name_Faculty '.' – Soufiane Feb 10 '21 at 17:36
  • As I said, clearly the code that changes the command doesn't work as it's using the columns from the wrong table. Best off just keeping to separate adapters for separate tables – Charlieface Feb 10 '21 at 17:39
  • I will try right now – Soufiane Feb 10 '21 at 17:48
  • You are the best The issue is found out. I don't now how I can thank you. – Soufiane Feb 10 '21 at 17:50
  • How I can talk with you ? Could you send your phone number or your Facebook account ? – Soufiane Feb 10 '21 at 17:54

1 Answers1

0

try this code :

            adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da);
            adoF2.da.Update(adoF2.ds,"disconnGrid");
            MessageBox.Show("Saving has been succeeded", "Info", MessageBoxButtons.OK);
  • Yes, I have used. The error was the arrangement of two codes into the form_Load event, if I reverse the arrangement the program will run without problems. – Soufiane Feb 12 '21 at 18:01