Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I’m trying to update one row of a SQL database from a DataTable.
In Variables.cs I have
public static DataSet PeopleDS = new DataSet();
        public static DataTable PeopleTable = PeopleDS.Tables.Add("PeopleTable");
        public static SqlDataAdapter PeopleDA;
In the main body I have:-
Variables.PeopleDA = new SqlDataAdapter();
Variables.PeopleDA.SelectCommand = new SqlCommand ("SELECT * FROM People" ,con0);
Variables.PeopleDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
Variables.PeopleDA.Fill(Variables.PeopleDS, "People");
Variables.PeopleTable = Variables.PeopleDS.Tables["People"];
After a selected person is found that row of the table is read into a DataRow variable:
   SelectString = "PersonID = " + WBID.ToString();
foundrows = Variables.PeopleTable.Select(SelectString);
   ThisRow = foundrows[0];
And values passed to a form:
textBox1.Text = ThisRow["FullName"].ToString();
            textBox3.Text = ThisRow["PersonalAddress"].ToString();
            textBox4.Text = ThisRow["EmailAddress"].ToString();
            textBox5.Text = ThisRow["EmailAddress2"].ToString();
            textBox6.Text = ThisRow["Details"].ToString();
            textBox7.Text = ThisRow["EmployeeNo"].ToString();
etc etc.
This form can then be edited and any changed values are passed back to a DataRow:
             ThisRow = Variables.PeopleTable.Select("PersonID=" + textBox17.Text).FirstOrDefault();
                    ThisRow["Title"] = comboBox2.SelectedItem;
                    ThisRow["CompanyID"] = Convert.ToInt16(comboBox3a.SelectedValue.ToString());
Etc etc
I’ve checked and so far so good, the DataTable has the changed values.
I can’t see how to run a SQL update for just this record – to find the correct row in the SQL table and simply copy the DataRow back. Having to write a SQL UPDATE statement listing every field would make DB changes require code changes.

What I have tried:

Started writing the complete UPDATE statement before realising its problems
Tried Variables.PeopleDA.Update(Variables.PeopleDS) -this gives System.InvalidOperationException
Posted
Updated 15-Oct-21 17:38pm

1 solution

As you wrote, one way is to write the SQL statements which means that you need to specify the fields in the statement. While this means a code change when the schema in the database changes, you may need to change code also for other reasons when a new column is added or an existing changed. For example, if the new column is included in some validations, participating in different kinds of business logic etc.

Having that said, you can also utilize a command builder to build the statements for you. This requires that sufficient metadata is present in the database, for example key information. For more information, have a look at SqlCommandBuilder Class (System.Data.SqlClient) | Microsoft Docs[^]

The capabilities of a data adapter (and command builder) are quite limited so if you need more complex logic in the data access layer, you may want to have a look at Entity Framework as an option. See Entity Framework Tutorial[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900