Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

Visual Studio® 2008:

ASP.NET 3.5
Module 3: Displaying and Manipulating Data in
ASP.NET 3.5
• Accessing Data by Using ADO.NET 3.5

• Accessing Data from Services

• Presenting Data in Web Controls


Lesson: Accessing Data by Using ADO.NET 3.5
• ADO.NET Classes

• Reading Data from a Database

• Modifying Data by Using a Data Set

• Working with XML Data


ADO.NET Classes

Data Provider

Connection Provides connectivity to a Data Source


Command Runs queries and executes other database commands
DataReader Provides high-performance read access to data
DataAdapter Loads data into DataSets and applies changes

DataSet

DataTable Contains rows and columns of data from a query


DataRelation Stores relationships between Data Tables
Reading Data from a Database

Connecting to a Database
Dim siteConnectionString as string ="Data
Source=.\sqlexpress;Initial Catalog=“ _
sitecontent;Integrated Security=True"
Dim siteConnection As SqlConnection = _
New SqlConnection(siteConnectionString)
siteConnection.Open()

Executing a DataReader
Dim command As SqlCommand = New SqlCommand()
command.Connection = siteConnection
command.CommandType = CommandType.Text
command.CommandText = _
"SELECT pageID, pageName FROM SitePages"

Dim reader As SqlDataReader = command.ExecuteReader()


Modifying Data by Using a DataSet

•DataSets contain one or more DataTable objects


•DataAdapter object provides Fill and Update methods to
manipulate DataSets

DataSet Object

DataTable Object DataTable Object

DataRow

Unique Data Unique


Constraint Column Constraint

ForeignKeyConstraint
DataRelation
Working with XML Data

DataSet Object XML File

DataTable Object

Loading Data from an XML File

Dim xmlFile As String = "C:\EmployeeDetails.xml"


Dim employeeDataSet As DataSet = New DataSet()
employeeDataSet.ReadXml(xmlFile, XmlReadMode.Auto)
Lesson: Accessing Data From Services
• XML Web Services and Windows Communication
Framework Services
• Working with Data from a Web Service

• Working with Data from a Windows Communication


Framework Service
XML Web Services and Windows Communication
Foundation Services
XML Web Services

• Provide a standard way to publish a service over HTTP


• Use XML for all messaging
• Publish their capabilities for service discovery
• Can be developed by using ASP.NET or other technologies

Windows Communication Foundation Services

• Publish services over many transport protocols


• Use many formats for messaging
• Publish their specifications as endpoints
• Can be developed by using .NET Framework 3.0 or later
Working with Data from a Web Service

• Discovering Web services

• Creating a Web service proxy class

• Instantiating the proxy class in an ASP.NET application

Sub Button1_Click(s As Object, e As EventArgs) _


Handles Button1.Click
Dim reviewsProxy As New _
Products.AWWebRef.ProductReviews()
numReviewsTextBox.Text = _
reviewsProxy.GetReviewsNumber("Racer")
End Sub
Working with Data from a Windows
Communication Foundation Service

• Create a client class

• Configure a client class

• Instantiate the proxy class


Lesson: Presenting Data in Web Controls
• What Is Data Binding?

• Connecting to Data by Using a DataSource Control

• Binding ASP.NET Controls to Data

• Modifying Data by Using DataSource Controls

• Demonstration: Data Binding in Visual Studio

• Data Binding Syntax


What Is Data Binding?

DataSource
Control

Database Data Bound Control


Connecting to Data by Using a DataSource
Control
• SqlDataSource control markup

<asp:SqlDataSource ID="SqlDataSource3" runat="server"


ConnectionString="Data Source=.\sqlexpress;
Initial Catalog=AdventureWorks;
Integrated Security=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [Title], [FirstName],
[LastName], [Phone] FROM [Contact]">
</asp:SqlDataSource>

• Store connection string in configuration file

<asp:SqlDataSource ID="SqlDataSource2" runat="server"


ConnectionString="<%$ ConnectionStrings:
AdventureWorksConnectionString %>"
SelectCommand="SELECT [Title], [FirstName],
[LastName], [Phone] FROM [Contact]">
</asp:SqlDataSource>
Binding ASP.NET Controls to Data
• GridView control markup

<asp:GridView ID="GridView1" runat="server"


AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="CultureID“
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CultureID"
HeaderText="CultureID" ReadOnly="True"
SortExpression="CultureID" />
<asp:BoundField DataField="Name“
HeaderText="Name“
SortExpression="Name" />
</Columns>
</asp:GridView>
Modifying Data by Using DataSource Controls

Data modification commands

• InsertCommand
• UpdateCommand
• DeleteCommand

Data modification events

• Inserting, Inserted
• Updating, Updated
• Deleting, Deleted
Demonstration: Data Binding in Visual Studio
In this demonstration, you will see how to add data-bound
controls to a Web page by using Visual Studio 2008
Data Binding Syntax

<asp:Label ID="currencycodeLabel" runat="server"


Text='<%# Eval("currencycode") %>' />

<asp:Label ID="nameLabel" runat="server"


Text='<%# Bind("name") %>' />

• Eval method

• Bind method

• Binding for a lookup table


Lab: Displaying and Manipulating Data in
ASP.NET 3.5
• Exercise 1: Accessing Data from an XML File as a Data
Source
• Exercise 2: Consuming Data from a Web Service

• Exercise 3: Displaying Data by Using Data Bound Controls

Logon information
Virtual machine 6463A-LON-DEV-03

User name Student


Password Pa$$w0rd

Estimated time: 60 minutes


Module Review and Takeaways
• Review Questions

• Real-world Issues and Scenarios

• Tools

You might also like