- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index
216 |
Sunday Morning |
The IndexOf method returns the index of the specified table. The method accepts either a string representing a DataTable’s name or a DataTable object as input parameters, as shown in the following example:
Dim iIndex As Integer
iIndex = oDS.Tables.IndexOf(“t_bands”)
The Clear method, as the name implies, removes all tables from the
DataTableCollection object.
CanRemove method
The CanRemove method returns a Boolean value indicating whether a specified table can be removed from the DataTableCollection object. The CanRemove method accepts a DataTable object as its input parameter.
Remove method
The Remove method removes the specified table from the DataTableCollection object. The Remove method can accept either a table’s name or a DataTable object as its input parameter. Here’s an example:
oDS.Tables.Remove(“t_bands”)
Add method
The Add method adds a table to the DataTableCollection. The Add method can be used in one of three ways. You can call the Add method and not pass it any input parameters. In this case a DataTable is added to DataTableCollection and assigned a default name. You can also pass only a string representing the table’s name. In this case, a DataTable is added to the DataTableCollection and assigned the specified name. Or you can call the Add method and pass it a DataTable object. In this case, the specified DataTable is added to the DataTableCollection. Here’s a quick example:
oDS.Tables.Add(“Bands”)
That’s it for the DataTableCollection object. Basically the DataTableCollection provides access to the DataTable object’s in a DataSet.
Now that you know how to get to your tables, let’s see what you can do with them.
DataTable Objects
The DataTable object is a central object in the ADO.NET library and effectively represents a data source’s data. You can manually fabricate a DataTable using its properties and methods, or you can have it automatically filled using DataSet commands.
Session 21—Introducing DataSets, Part II |
217 |
In order to manually construct a DataTable we can use the following code:
Dim oDT As DataTable
oDT = New DataTable()
You can also pass a string representing the DataTable’s name to the constructor as follows:
oDT = New DataTable(“MyTable”)
As will all ADO.NET objects, the DataTable object has a variety of properties and methods. I’ll start with the properties.
CaseSensitive property
The CaseSensitive property is a Boolean value that indicates whether string comparison within a table is case sensitive. The property’s default value is set to the parent DataSet object’s CaseSensitive property. The CaseSensitive property affects string comparisons in sorting, searching, and filtering. Here’s an example:
oDS.Tables.Item(0).CaseSensitive = False
In this statement you are going through a DataSet object’s Tables property to access a DataTableCollection object. You will then use the DataTableCollection’s Item property to access the DataTable in the collection with an index of zero. Once you have drilled down to the DataTable, you can access its properties, in this case the CaseSensitive property.
ChildRelations property
If you have more than one table in your DataSet, chances are that you’ll want to relate them in some way. The ChildRelations property gets a collection of child relations for a DataTable in the form of a DataRelationCollection object. This is a little complicated and we can’t go into it too much in this session. Data relationships are further discussed in Session 25, “Data Shaping with ADO.NET.”
Columns property
The Columns property gets the collection of columns that belong to a DataTable in the form of a DataColumnCollection object. The relationship between the Columns property and a DataTable is very similar to the relationship between the Tables property and a DataSet. The Columns property exposes a DataColumnCollection through which you can access a DataTable’s columns. Here’s sample syntax demonstrating how you can access a DataTable’s columns using the Columns property:
Dim oDCC As DataColumnCollection oDCC = oDS.Tables(0).Columns
218 |
Sunday Morning |
Constraints property
The Constraints property gets the collection of constraints maintained by a DataTable object in the form of a ConstraintCollection object. If you’ll recall from our discussion on database design in Session 3, “Designing A Database,” a table can have zero, one, or multiple constraints. Some typical constraints you’ll find on a given table are foreign key and unique constraints. We’ll return to the Constraints property in Session 25, “Data Shaping with ADO.NET.”
DataSet property
If a DataTable belongs to a DataSet, the DataSet property returns a reference to the parent DataSet. You might find this property useful when processing a form. In the following code snippet, assume that you have submitted an HTML form that contains a DataGrid control, dgExample, which is bound to a DataTable. Here’s the syntax:
Dim oDS As DataSet
Dim oDT As DataTable
oDT = cType(dgExample.DataSource, DataTable) oDS = oDT.DataSet
DefaultView property
The DefaultView property gets a customized view of the DataTable in the form of a DataView object. A DataView object is a databindable, customized view of a DataTable used for sorting, filtering, searching, editing, and navigating a DataTable. We’ll talk more about this in Session 22, “Introducing Data Binding.”
ParentRelations property
The ParentRelations property is very similar to the ChildRelations property except that it gets the parent relationships rather than the child relationships. (We guess that kind of makes sense.) The ParentRelations property gets a collection of parent relations for a DataTable in the form of a DataRelationCollection object. More in Session 25, “Data Shaping with ADO.NET.”
PrimaryKey property
The PrimaryKey property gets or sets an array of columns that function as primary keys for a DataTable. In most cases you’ll be getting the primary key columns rather than setting them. In cases where you need to manually create a custom DataTable object, you’ll set the PrimaryKey property. The PrimaryKey property returns an array of DataColumn objects. Likewise, when you are setting the PrimaryKey property, you need to pass it an array of DataColumn objects. Here’s an example of getting the primary key columns on a DataTable:
Session 21—Introducing DataSets, Part II |
219 |
Dim aPK() As DataColumn
Dim x As Integer
oDT = oDS.Tables(“t_bands”) aPK = oDT.PrimaryKey
For x = LBound(aPK) to UBound(aPK)
Response.Write(“PRIMARY KEY “ & x & “: “ & aPK(x).ColumnName & “<BR/>” & chr(13))
Next
In this example, you gain access to the Tables collection via the DataSet and then initialize an array, aPK, using the DataTable object’s PrimaryKey property. We then use a For loop to iterate through the array.
Rows property
The Rows property provides access to the collection of rows that belong to the table. This property is similar to the Columns property, but instead of returning a collection of DataColumn objects, it returns a collection of DataRow objects. It is through the Rows property that you can gain access to a DataTable’s constituent DataRow objects’ properties and methods. A DataTable’s DataColumn objects represent the DataTable’s structure whereas the DataRow objects represent the DataTable’s data.
Additionally, it is through the DataRow object that you can gain access to the data in your DataTable objects. Listing 21-2 demonstrates iterating through the DataRowCollection (returned by the Rows property) in a DataTable and writing its contents to a .NET Table control.
Listing 21-2 Iterating through a table’s rows
<%@ Page Language=”VB” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.OleDb” %> <SCRIPT LANGUAGE=”VB” RUNAT=”server”>
Sub Page_Load(Sender As Object, E As EventArgs) Dim oConn As OleDbConnection
Dim oDA As OleDbDataAdapter Dim oDS As DataSet
Dim oDT As DataTable
Dim oDR As DataRow Dim oDC As DataColumn Dim oTR As TableRow
Dim oTHC As TableHeaderCell Dim oTC As TableCell
oConn = New OleDbConnection(“Provider=SQLOLEDB;Data
Source=(local);Initial Catalog=Music;User ID=music;Password=music”)
oConn.Open
Continued
220 |
Sunday Morning |
Listing 21-2 |
Continued |
oDA = New OleDbDataAdapter(“SELECT * FROM t_bands”, oConn)
oDS = New DataSet(“Music”)
oDA.Fill(oDS, “t_bands”)
oDT = oDS.Tables(“t_bands”)
oTR = New TableRow
oTR.BackColor = System.Drawing.Color.LightGray For Each oDC In oDT.Columns
oTHC = New TableHeaderCell oTHC.Text = oDC.ColumnName oTR.Cells.Add(oTHC)
Next tblBands.Rows.Add(oTR)
For Each oDR In oDT.Rows oTR = New TableRow
For Each oDC In oDT.Columns
oTC = New TableCell oTC.Text = oDR.Item(oDC) oTR.Cells.Add(oTC)
Next tblBands.Rows.Add(oTR)
Next
oTC.Dispose oTC = Nothing oTR.Dispose oTR = Nothing oDT.Dispose oDT = Nothing oDS.Dispose oDS = Nothing oDA.Dispose oDA = Nothing oConn.Close oConn = Nothing
End Sub </SCRIPT> <HTML> <BODY>
<asp:Table id=”tblBands” BorderWidth=”1” GridLines=”both” runat=”server”/> </BODY>
</HTML>