CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
MainForm.cs using the Solution Explorer. After you create the project, set a reference to your updated
AutoLotDAL.dll assembly (be sure you pick version 2.0.0.0!) and import the following namespace:
using AutoLotDisconnectedLayer;
The design of the form consists of a single Label, DataGridView (named inventoryGrid), and Button
control (named btnUpdateInventory), which you configure to handle the Click event. Here is the
definition of the form:
public partial class MainForm : Form
{
InventoryDALDisLayer dal = null;
public MainForm()
{
InitializeComponent();
string cnStr =
@"Data Source=(local)\SQLEXPRESS;Initial Catalog=AutoLot;" +
"Integrated Security=True;Pooling=False";
// Create our data access object.
dal = new InventoryDALDisLayer(cnStr);
}
// Fill up our grid!
inventoryGrid.DataSource = dal.GetAllInventory();
private void btnUpdateInventory_Click(object sender, EventArgs e)
{
// Get modified data from the grid.
DataTable changedDT = (DataTable)inventoryGrid.DataSource;
}
}
try
{
// Commit our changes.
dal.UpdateInventory(changedDT);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
After you create the InventoryDALDisLayer object, you can bind the DataTable returned from
GetAllInventory() to the DataGridView object. When the user clicks the Update button, you extract the
modified DataTable from the grid (with the DataSource property) and pass it into your UpdateInventory()
method.
That’s it! After you run this application, add a set of new rows to the grid and update/delete a few
others. Assuming you click the Button control, you will see your changes have persisted into the AutoLot
database.
895