Workshop

for RuBoard

This workshop will help reinforce the concepts covered in today's lesson.

Quiz

1:

What is the purpose of the CurrencyManager object on a Windows Form?

A1:

The CurrencyManager object manages the position and state of a data source on a Windows Form. This is required because the data source itself has no concept of an internal cursor.

2:

Are there any caveats to setting up data binding on a Windows Form?

A2:

The biggest caveat is that you must make certain that the data source you specify when adding bindings to the BindingCollection object is the same for all controls. Using a different expression by, for example, specifying just the DataSet in one case and both the DataSet and DataTable in the other, will result in two CurrencyManager objects being created. The end result will be that the controls won't be synchronized when navigation occurs.

3:

What are the assumptions you need to consider when using Web Forms data binding?

A3:

Data binding in Web Forms assumes that most binding is read-only and so it does not support automatically updating a data source with data from the bound controls. In addition, it adheres to the stateless nature of the Web by not trying to manage the position of a data source as the CurrencyManager does in Windows Forms.

4:

What are some techniques for minimizing the roundtrips to the data store when using data binding in Web Forms?

A4:

In order to reduce the traffic to the database server when using binding in Web Forms, you can opt to store the data in session state ( HttpSessionState object), view state, application state ( HttpApplicationState object), or the Cache object.

Each has its advantages for particular scenarios, although session state is the most flexible. This is because session state enables you to cache data on a per-user basis that is available to all pages accessed by that user and can be stored externally to the Web server to support Web farm scenarios.

Exercise

Q1:

Build a simple Windows Forms application that contains a DataGrid and controls for several of the columns in the Titles table in the ComputeBooks database. Ensure that as you scroll through the DataGrid , the values in the other controls track with the current row.

A1:

One possible solution is to create a simple form that retrieves a DataSet , creates a DataView , and then binds the control to the DataView as shown in Listing 16.9 (note that the Web Form designer generated code is omitted).

Listing 16.9 Binding to titles. This form binds a DataView created from a DataSet to both a DataGrid and other controls.
 Imports System.Data.SqlClient Public Class TitlesForm   Inherits System.Windows.Forms.Form   Protected dvTitles As DataView   Private Sub TitlesForm_Load(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles MyBase.Load     Dim con As New SqlConnection( _      "server=ssosa;database=computebooks;trusted_connection=yes")     Dim da As New SqlDataAdapter("usp_GetTitles", con)     Dim dsTitles As New DataSet()     da.SelectCommand.CommandType = CommandType.StoredProcedure     da.MissingSchemaAction = MissingSchemaAction.AddWithKey     Try       da.Fill(dsTitles)       dvTitles = New DataView(dsTitles.Tables(0))       DataGrid1.DataSource = dvTitles     Catch sqlE As SqlException       MsgBox(sqlE.Message)     End Try     ' Set up bindings     txtISBN.DataBindings.Add("Text", dvTitles, "ISBN")     txtTitle.DataBindings.Add("Text", dvTitles, "Title")     txtDesc.DataBindings.Add("Text", dvTitles, "Description")     txtPrice.DataBindings.Add("Text", dvTitles, "Price")     txtAuthor.DataBindings.Add("Text", dvTitles, "Author")     dtPubDate.DataBindings.Add("Value", dvTitles, "PubDate")     AddHandler txtPrice.DataBindings(0).Format, AddressOf Me._formatCurrency   End Sub   Private Sub _formatCurrency(ByVal sender As Object, _     ByVal e As ConvertEventArgs)     If e.DesiredType Is GetType(String) Then       e.Value = Format(e.Value, "currency")     End If   End Sub End Class 

Figure 16.5 shows the resulting form.

Figure 16.5. Binding to titles. This form uses both simple and complex binding to display data from a DataView object.

graphics/16fig05.jpg

for RuBoard


Sams Teach Yourself Ado. Net in 21 Days
Sams Teach Yourself ADO.NET in 21 Days
ISBN: 0672323869
EAN: 2147483647
Year: 2002
Pages: 158
Authors: Dan Fox

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net