1. * Select (left click) sqlDataAdapter1 at bottom of form design screen.
* In the properties window, expand the "SQL Command" property.
* Click the "CommandText" button to open the "Query Builder" window.
* Click the "ContactTitle" checkbox in the "Customers" table listbox.
* Click "OK" button
* Right click sqlDataAdapter1 at bottom of form design screen.
* Select "Generate Dataset..." from menu.
* "Existing" radio button should be selected with "GettingData.dsCustomers".
* "Tables to add" listbox should have "Customers" checkbox checked.
* Click "OK" button
-OR-
* Run the wizard from
"Data | Generate Dataset..." menu selections
* Follow instructions above at appropriate screens
2. From the toolbox, double click the textbox control (and a label for consistency)
Code:
Control Property Value
+-----------------+---------------+-----------------------+
|TextBox |Name |txtContactTitle |
| |Text |(blank) |
| |DataBind/Text |dsCustomers1 - |
| | |Customers.ContactTitle |
+-----------------+---------------+-----------------------+
|Label |Name |lblContactTitle |
| |Text |Contact Title |
+-----------------+---------------+-----------------------+
3. Sorry. Instead of rewriting the chapter here with different datasource names,
you must learn this one by doing
4. The code change will point to top/bottom row rather than previous/next result.
private void cmdNext_Click(object sender, System.EventArgs e)
{
<s>// Move to the next record in the Dataset, Customers table</s>
<s>//this.BindingContext[this.dsCustomers1,"Customers"].Position++;</s>
// Move to the last record in the Dataset, Customers table
this.BindingContext[this.dsCustomers1,"Customers"].Position =
this.BindingContext[this.dsCustomers1,"Customers"].Count -1;
... Rest of code
}
private void cmdBack_Click(object sender, System.EventArgs e)
{
<s>// Move to the previous record in the Dataset, Customers table</s>
<s>// this.BindingContext[this.dsCustomers1,"Customers"].Position--;</s>
// Move to the first record in the Dataset, Customers table
this.BindingContext[this.dsCustomers1,"Customers"].Position = 0;
... Rest of code
}
5. Extracted some comments enclosed in "summary" XML tags
/// <summary>
/// This class assembled in Chapter 18 demonstrates data source and data binding techniques
/// </summary>
public class frmMainLoad : System.Windows.Forms.Form
{
/// <summary>
/// TextBox control to handle customers.CustomerID [Chapter 18-Page 572]
/// </summary>
private System.Windows.Forms.TextBox txtCustID;
/// <summary>
/// TextBox control to handle customers.CompanyName [Chapter 18-Page 580]
/// </summary>
private System.Windows.Forms.TextBox txtCustName;
/// <summary>
/// TextBox control to handle customers.ContactName [Chapter 18-Page 580]
/// </summary>
private System.Windows.Forms.TextBox txtContactName;
/// <summary>
/// TextBox control to handle customers.ContactTitle [Chapter 18-Page 602]
/// </summary>
private System.Windows.Forms.TextBox txtContactTitle;
/// <summary>
/// Label control to identify customers.CustomerID [Chapter 18-Page 572]
/// </summary>
private System.Windows.Forms.Label lblCustID;
/// <summary>
/// Label control to identify customers.CompanyName [Chapter 18-Page 580]
/// </summary>
private System.Windows.Forms.Label lblCustName;
/// <summary>
/// Label control to identify customers.ContactName [Chapter 18-Page 580]
/// </summary>
private System.Windows.Forms.Label lblContactName;
/// <summary>
/// Label control to identify customers.ContactTitle [Chapter 18-Page 602]
/// </summary>
private System.Windows.Forms.Label lblContactTitle;
/// <summary>
/// Data adapter control to connect to SQL data source [Chapter 18-Page 573]
/// </summary>
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
... Code removed for brevity
/// <summary>
/// Method handles back button click event [Chapter 18-Page 582]
/// </summary>
private void cmdBack_Click(object sender, System.EventArgs e)
{
// Move to the previous record in the Dataset, Customers table
// this.BindingContext[this.dsCustomers1,"Customers"].Position--;
this.BindingContext[this.dsCustomers1,"Customers"].Position = 0;
// Synchronize the listbox pointer to the DataSet
this.lstCustID.SelectedIndex =
this.BindingContext[this.dsCustomers1,"Customers"].Position;
}
/// <summary>
/// Method handles listbox selected index changed event [Chapter 18-Page 583]
/// </summary>
private void lstCustID_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Pass the new index number to the binding context
this.BindingContext[this.dsCustomers1,"Customers"].Position =
this.lstCustID.SelectedIndex;
}
/// <summary>
/// Method writes to SQL data source on update button click event [Chapter 18-Page 587]
/// </summary>
private void cmdUpdate_Click(object sender, System.EventArgs e)
{
// Pass the DataSet back to the database
int rowsUpdated = this.sqlDataAdapter1.Update(this.dsCustomers1.Cust omers);
MessageBox.Show(rowsUpdated.ToString() +
((rowsUpdated==1) ? " row" : " rows") + " updated.");
}
}
Run
"Tools| Build Comment Web Pages..." for the web pages