ASP to Aspnet Migration

Migration Overview Syntax and Semantics Language Compatibility COM Interoperability Transactions

VB DataBind3.aspx

The following example illustrates binding to a DataView. Note that the DataView class is defined in the System.Data namespace.

VB DataBind4.aspx

The following example illustrates binding to a Hashtable.

VB DataBind5.aspx

Binding Expressions or Methods

Often, you'll want to manipulate data before binding to your page or a control. The following example illustrates binding to an expression and the return value of a method.

VB DataBind6.aspx


The ASP.NET framework supplies a static method that evaluates late-bound data binding expressions and optionally formats the result as a string. DataBinder.Eval is convenient in that it eliminates much of the explicit casting the developer must do to coerce values to the desired data type. It is particularly useful when data binding controls within a templated list, because often both the data row and the data field must be cast.

Consider the following example, where an integer will be displayed as a currency string. With the standard ASP.NET data binding syntax, you must first cast the type of the data row in order to retrieve the data field, IntegerValue. Next, this is passed as an argument to the String.Format method.

<%# String.Format("{0:c}", (Container.DataItem.("IntegerValue") %>

This syntax can be complex and difficult to remember. In contrast, DataBinder.Eval is simply a method with three arguments: the naming container for the data item, the data field name, and a format string. In a templated list like DataList, DataGrid, or Repeater, the naming container is always Container.DataItem. Page is another naming container that can be used with DataBinder.Eval.

<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>

The format string argument is optional. If it is omitted, DataBinder.Eval returns a value of type object, as shown in the following example.

<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>

It is important to note that DataBinder.Eval can carry a noticeable performance penalty over the standard data binding syntax because it uses late-bound reflection. Use DataBinder.Eval judiciously, especially when string formatting is not required.

VB DataBind7.aspx fRun Sample! | fView Source!

Section Summary

1. The ASP.NET declarative data binding syntax uses the <%# %> notation.

2. You can bind to data sources, properties of the page or another control, collections, expressions, and results returned from method calls.

3. List controls can bind to collections that support the ICollection, IEnumerable, or IListSource interface, such as ArrayList, Hashtable, DataView, and DataReader.

4. DataBinder.Eval is a static method for late binding. Its syntax can be simpler than the standard data binding syntax, but performance is slower.

Getting Started

Introduction What is ASP.NET? Language Support

ASP.NET Web Forms

Introducing Web Forms Working with Server Controls Applying Styles to Controls Server Control Form Validation Web Forms User Controls Data Binding Server Controls Server-Side Data Access Data Access and Customization Working with Business Objects Authoring Custom Controls Web Forms Controls Reference Web Forms Syntax Reference

ASP.NET Web Services

Introducing Web Services Writing a Simple Web Service Web Service Type Marshalling Using Data in Web Services Using Objects and Intrinsics The WebService Behavior HTML Pattern Matching

ASP.NET Web Applications

Application Overview Using the Global.asax File Managing Application State HttpHandlers and Factories

Cache Services

Caching Overview Page Output Caching Page Fragment Caching Page Data Caching

Server-Side Data Access

■ Introduction to Server-Side Data

■ Connections, Commands, and DataSets

■ Accessing SQL-based Data

■ Binding SQL Data to a DataGrid

■ Performing a Parameterized Select

■ Inserting Data in a SQL Database

■ Updating Data in a SQL Database

■ Deleting Data in a SQL Database

■ Sorting Data from a SQL Database

■ Working with Master-Detail Relationships

■ Writing and Using Stored Procedures

■ Accessing XML-based Data

■ Section Summary

Introduction to Server-Side Data

Data access is the heart of any real-world application, and ASP.NET provides a rich set of controls that are well-integrated with the managed data access APIs provided in the common language runtime. This section walks through several iterations of a sample that uses the ASP.NET DataGrid control to bind to the results of SQL queries and XML data files. This section assumes some familiarity with database fundamentals and the SQL query language.

Server-side data access is unique in that Web pages are basically stateless, which presents some difficult challenges when trying to perform transactions such as inserting or updating records from a set of data retrieved from a database. As you'll see in this section, the DataGrid control can help manage these challenges, allowing you to concentrate more on your application logic and less on the details of state management and event handling.

Connections, Commands, and Datasets

The common language runtime provides a complete set of managed data access APIs for data-intensive application development. These APIs help to abstract the data and present it in a consistent way regardless of its actual source (SQL Server, OLEDB, XML, and so on). There are essentially three objects you will work with most often: connections, commands, and datasets.

• A connection represents a physical connection to some data store, such as SQL Server or an XML file.

• A command represents a directive to retrieve from (select) or manipulate (insert, update, delete) the data store.

• A dataset represents the actual data an application works with. Note that datasets are always disconnected from their source connection and data model and can be modified independently. However, changes to a dataset can be easily reconciled with the originating data model.

For a more detailed walkthrough of the managed data access solution in the common language runtime, please read the ADO.NET Overview section of this tutorial.


Configuration Overview Configuration File Format Retrieving Configuration


Deploying Applications Using the Process Model Handling Errors


Security Overview Authentication & Authorization Windows-based Authentication Forms-based Authentication Authorizing Users and Roles User Account Impersonation Security and WebServices


Internationalization Overview Setting Culture and Encoding Localizing ASP.NET Applications Working with Resource Files


Tracing Overview

Trace Logging to Page Output

Application-level Trace Logging


The SDK Debugger


Performance Overview Performance Tuning Tips Measuring Performance

Accessing SQL-based Data

An application typically needs to perform one or more select, insert, update, or delete queries to a SQL database. The following table shows an example of each of these queries.



Simple Select

SELECT * from Employees WHERE FirstName = 'Bradley';

Join Select

SELECT * from Employees E, Managers M WHERE E.FirstName = M.FirstName;


INSERT into Employees VALUES ('123-45-6789','Bradley','Millington','Program Manager');


UPDATE Employees SET Title = 'Development Lead' WHERE FirstName = 'Bradley';


DELETE from Employees WHERE Productivity < 10;

To give your page access to the classes you will need to perform SQL data access, you must import the System.Data and System.Data.SqlClient namespaces into your page.

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

To perform a select query to a SQL database, you create a SqlConnection to the database passing the connection string, and then construct a SqlDataAdapter object that contains your query statement. To populate a DataSet object with the results from the query, you call the command's Fill method.

Dim myConnection As New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes") Dim myCommand As New SqlDataAdapter("select * from Authors", myConnection)

Dim ds As New DataSet() myCommand.Fill(ds, "Authors")

As mentioned earlier in this section, the benefit of using a dataset is that it gives you a disconnected view of the database. You can operate on a dataset in your application, and then reconcile your changes with the actual database later. For long-running applications this is often the best approach. For Web applications, you are usually performing short operations with each request (commonly to simply display the data). You often don't need to hold a DataSet object over a series of several requests. For situations like these, you can use a SqlDataReader.

A SqlDataReader provides a forward-only, read-only pointer over data retrieved from a SQL database. Because SqlDataReader uses a Tabular Data Stream (TDS) to read data directly from a database connection, it performs more effectively than a DataSet if it can be used for your scenario.

To use a SqlDataReader, you declare a SqlCommand instead of a SqlDataAdapter. The SqlCommand exposes an ExecuteReader method that

ASP to ASP.NET Migration (2 of 14) [4/02/2002 8:50:51 AM]

Migration Overview Syntax and Semantics Language Compatibility COM Interoperability Transactions

Sample Applications

A Personalized Portal An E-Commerce Storefront A Class Browser Application returns a SqlDataReader. Note also that you must explicitly open and close the SqlConnection when you use a SqlCommand. After a call to ExecuteReader, the SqlDataReader can be bound to an ASP.NET server control, as you'll see in the next section.

Dim myConnection As SqlConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes") Dim myCommand As SqlCommand = New SqlCommand("select * from Authors", myConnection)


Dim dr As SqlDataReader = myCommand.ExecuteReader()


Get URL for this page

When performing commands that do not require data to be returned, such as inserts, updates, and deletes, you also use a SqlCommand. The command is issued by calling an ExecuteNonQuery method, which returns the number of rows affected. Note that the connection must be explicitly opened when you use the SqlCommand; the SqlDataAdapter automatically handles opening the connection for you.

Dim myConnection As New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes") Dim myCommand As New SqlCommand( _

"UPDATE Authors SET phone='(800) 555-5555' WHERE au_id = '12 3-45-6789'", myConnection)

myCommand.Connection.Open() myCommand.ExecuteNonQuery() myCommand.Connection.Close()

Important: Always remember to close the connection to the data model before the page finishes executing. If you do not close the connection, you might inadvertently exhaust the connection limit while waiting for the page instances to be handled by garbage collection.

Was this article helpful?

0 0

Post a comment