Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2004, 03:54 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 18

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
 
Old October 9th, 2004, 11:33 PM
Friend of Wrox
 
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What is the question here?



It is not how much we do,
but how much love we put in the doing.

-Mother Theresa





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 12 seblake C# 1 August 21st, 2004 02:55 PM
Beginning Visual C# Exercises - Chapter 05 seblake C# 1 July 26th, 2004 07:40 AM
Beginning Visual C# Exercises - Chapter 04 seblake C# 0 July 21st, 2004 09:21 AM
Beginning Visual C# Exercises - Chapter 07 seblake C# 0 July 20th, 2004 02:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.