It looks like you are doing a lot of manual work that could be facilitated by linking your controls directly to your DataSet / Table. Databinding combines your data source (your dataset / table in this case) with your user interface. When a value is changed in the user interface, it will update the data source.
DataBinding is a BIG topic that requires research and testing. There are some getcha with data binding to DataTable / Set (row changes are not obtained until the current row changes, which is annoying in your case, only working with one row at a time, but there are workarounds).
Paraphrased: Another thing to consider is to use business objects to represent the data in your sets / tables. ORMs (object-relational-mappers) can handle this for you, but they are large and extremely powerful frameworks that are not easy to master. This is a completely different paradigm for working with DataSet / Tables at the user interface level and is more faithful to object-oriented programming. DataSets and Tables are very good for working with tabular data, but they do not work well with objects. For example, you should work against an instance of the Person object with properties like IsHispanic and IsCitizen rahtner, than essentially work with cells in the table (no more than * myPersonTable [0] ["HispanicOriginFlag"] ....).
Next: Not related to your question, but related to CRUD operations revolving around ADO.NET: it pays to familiarize yourself with the state tracking built into the DataTable / DataSet. There are many built-in ADO.NET to help these applications stick together easily, which will clear tons of code, as shown in the figure.
As always, RAD tools have a trade-off that allows you to give up performance control, but writing them off without understanding them ensures that you spend your days writing code, as you showed.
Even more: To continue working on my previous Next, when you discover the possibility of combining the Visual Studio DataSet generator with the built-in row tracking in DataTables and tracking DataSets changes, it can very easily write a complete CRUD system in a short time.
The following is a brief description of some of the steps:
- Install Database Schema
- In Visual Studio, add a new DataSet to the project.
- Find the server explorer (in the view)
- Add SQL Server as a Data Connection
- Drag the table / saved proc / View into the DataSet constructor.
- Right-click the "TableAdapter" that Visual Studio created for you; go to "Customize"
- Configure CRUD Commands for the DataSet (Select, Insert, Update, Delete Commands)
With this, you created a highly typed DataSet. The DataSet will contain a DataTable property named after the table / view / stored procedure used to generate the DataSet. The Table property will contain strongly typed strings that will allow you to access the cells inside this row as properties, rather than untyped elements in an array of objects.
So, if you created a new DataSet called MyDbTables with a table called tblCustomer that contains some columns, such as CustomerId, Name, etc., then you can work with it as follows:
This is a lot of examples, rolled into one, showing some of the common methods used to work with CRUD - check out the methods and especially in the TableAdapter class.
public void MyDtDemo() {
Also note the RejectChanges () and AcceptChanges () methods of both the DataSet and DataTables. They essentially tell your dataset that it has no changes (either rejecting all the changes or "making" all the changes), but keep in mind that calling AcceptChanges () and then trying to update will have no effect - DataSet lost track of any changes and suggests that this is an accurate reflection of the database.
And even more! Here's a revised version of your example showing some rowstate tracking functions, assuming you followed my steps to create strongly typed DataSets / Tables / Rows
public void CheckRows() { MyPersonDS tmpPersonDS = new MyPersonDS(); // Load Person info using (var tmpPersonDT = tmpPersonDS.PersonDT) { foreach (MyPersonRow row in tmpPersonDT.Rows) { CheckPersonData(row); } } } public void CheckPersonData(MyPersonRow row) { // If DataBinding is used, then show if the row is unchanged / modified / new... System.Diagnostics.Debug.WriteLine("Row State: " + row.RowState.ToString()); System.Diagnostics.Debug.WriteLine("Row Changes:"); System.Diagnostics.Debug.WriteLine(BuildRowChangeSummary(row)); // If not DataBound then update the strongly-types Row properties row.ResidencyCountyID = lkuResidencyCountyId.EditValue; } public string BuildRowChangeSummary(DataRow row) { System.Text.StringBuilder result = new System.Text.StringBuilder(); int rowColumnCount = row.Table.Columns.Count; for (int index = 0; index < rowColumnCount; ++index) { result.Append(string.Format("Original value of {0}: {1}\r\n", row.Table.Columns[index].ColumnName, row[index, DataRowVersion.Original])); result.Append(string.Format("Current value of {0}: {1}\r\n", row.Table.Columns[index].ColumnName, row[index, DataRowVersion.Current])); if (index < rowColumnCount - 1) { result.Append("\r\n"); } } return result.ToString(); }