Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] - نسخه متنی

Jason Beres

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Exercises



  1. In the DataAccess application you wrote today, you added a button named ShowCheckedItems to TabPage1. Write the code that iterates through the CheckedListBox and return the items that are checked. To get started, look up the GetItemChecked method in the SDK. This should give you a hint about how to proceed with the code.


  2. We didn''t delve too much into the Command.Parameters collection today. Being able to send parameters to a stored procedure is extremely important. To get familiar with the syntax, examine the following Visual Basic .NET code:


    With cmd.Parameters
    .Add("@au_id", SqlDbType.VarChar, 11).Value = TextBox1.Text
    .Add("@au_fname", SqlDbType.VarChar, 20).Value = TextBox2.Text
    .Add("@au_lname", SqlDbType.VarChar, 20).Value = TextBox3.Text
    .Add("@phone", SqlDbType.Char, 12).Value = TextBox4.Text
    .Add("@address", SqlDbType.VarChar, 40).Value = TextBox5.Text
    .Add("@city", SqlDbType.VarChar, 20).Value = TextBox6.Text
    .Add("@state", SqlDbType.Char, 2).Value = TextBox7.Text
    .Add("@zip", SqlDbType.Char, 5).Value = TextBox8.Text
    .Add("@contract", SqlDbType.Int).Value = "1"
    End With

    Now, open up the completed DataAccess solution and study the code in the AddAuthor_Click event. This is the model for adding parameters to the Command object and passing them to a stored procedure. In the SDK file, look up the SqlDbType enumeration, and learn why it''s important when dealing with creating parameters and how the data types in .NET map to the data types in SQL Server. After tomorrow ''s exercises, you''ll have a better understanding of parameters, and you''ll be using them more throughout the week.


  3. When you added the ComboBox to TabPage2, you didn''t write any code for it. You''re now going to write code that


    • Binds the Customers table from the Northwind database to the ComboBox2 on TabPage2


    • Filters the DataGrid based on the CustomerID that''s selected from ComboBox2


    To get started, add a DataView variable named dv directly under the Public Class Form1 declaration. Your code should look like this:


    public class Form1 : System.Windows.Forms.Form
    {
    private DataView dv;

    Or, if you''re using Visual Basic .NET, your code should look like this:


    Public Class Form1
    Inherits System.Windows.Forms.Form
    Private dv As DataView

    The DataView object is reused in several Form1 methods, so making it globally available to the class is important.

    Next, create a method in your code called Load_Customers. The Load_Customers method mimics the code you wrote for the BindWithDataSet_Click event except it works with the Northwind database and the Customers table, not the Authors table in the Pubs database.

    In the Load_Customers method, do the following:


    • Create a connection to the Northwind database.


    • Create a SqlDataAdapter that selects all Customers from the Customers table.


    • Call the Fill method of the SqlDataAdapter using the name Customers as the DataTable for the DataSet.


    • Set the ComboBox2 DataSource to the DataSet you just created.


    • Set the ValueMember of the ComboBox to the CustomerID field.


    • Set the DisplayMember to the ComboBox to the CustomerID field.


    Next you must write code in the SelectedIndexChanged event of ComboBox2 that uses the RowFilter event of the DataView object to filter the DataView and rebinds the DataView to the DataGrid.

    The following code accomplishes that task:


    Private Sub ComboBox2_SelectedIndexChanged
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles
    ComboBox2.SelectedIndexChanged
    '' Set the RowFilter property on the DataView
    dv.RowFilter = "CustomerID Like ''" & ComboBox2.Text & "%''"
    '' Re-set the datasource to the filtered data
    DataGrid1.DataSource = dv
    End Sub

    private void comboBox2_SelectedIndexChanged
    (object sender, System.EventArgs e)
    {
    // Set the RowFilter property on the DataView
    dv.RowFilter = "CustomerID Like ''" + comboBox2.Text + "%''";
    // Re-set the datasource to the filtered data
    dataGrid1.DataSource = dv;
    }

    To finish up, modify the code you wrote earlier in the TabControl1_TabIndexChanged event to bind the DataGrid to the DataView instead of the DataSet.

    The following code hint will help you do this:


    dv = New DataView(ds.Tables(0), ", ",
    DataViewRowState.OriginalRows)

    To get a better understanding of the DataView object, look up DataView in the SDK and explore its capabilities.

    When you''re done, you have a complete mechanism for filtering records in a DataSet using the DataView object.

    Your application should look something like Figure 10.10 when you are finished.

    Figure 10.10. The results from exercise 3.




/ 270