Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

DataBinding in ASP.NET 2.0
Author: Unknown
Published: 7/23/2005 3:55 PM
Category: ASP.NET, Authentication, AWS, Database, Datalist, DTD, ExceptionHandling, Schema, Security, SQL, VB.NET, VS.NET, XHTML, XML
Summary: This enables developers
To configure html error pages to be displayed in place of an error stack trace, Customers base, suppliers utilizes the BookStores.XML and the Countries.XML

 

1)DataBinding\Code\CustomerDB.vb

Imports System

Imports System.Data

Imports System.Data.SqlClient

' <summary>

' Fetches CustomerID and CompanyName from Northwind Customers

' </summary>

Public Class CustomerDB

Public Function GetCustomers() As System.Data.DataSet

Dim strCnn As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=localhost"

Dim cnn As New SqlConnection(strCnn)

Dim sqlDA As New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", cnn)

Dim ds As New DataSet()

sqlDA.Fill(ds, "Customers")

Return ds

End Function

End Class

2)DataBinding\Code\GenericCustomer.vb

Imports System

Imports System.Data

Imports System.Data.SqlClient

' <summary>

' Fetches CustomerID and CompanyName from Northwind Customers

' </summary>

Public Class CustomerDB

Public Function GetCustomers() As System.Data.DataSet

Dim strCnn As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=localhost"

Dim cnn As New SqlConnection(strCnn)

Dim sqlDA As New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", cnn)

Dim ds As New DataSet()

sqlDA.Fill(ds, "Customers")

Return ds

End Function

End Class

3)DataBinding\Data\Bookstore.xml

<Bookstore>

<genre name="Fiction">

<book ISBN="10-861003-324" Title="A Tale of Two Cities" Price="19.99">

<chapter num="1" name="Introduction">

Abstract...

</chapter>

<chapter num="2" name="Body">

Abstract...

</chapter>

<chapter num="3" name="Conclusion">

Abstract...

</chapter>

</book>

<book ISBN="1-861001-57-5" Title="Pride And Prejudice" Price="24.95">

<chapter num="1" name="Introduction">

Abstract...

</chapter>

<chapter num="2" name="Body">

Abstract...

</chapter>

<chapter num="3" name="Conclusion">

Abstract...

</chapter>

</book>

<book ISBN="1-861001-57-6" Title="The Old Man and the Sea" Price="27.95">

<chapter num="1" name="Introduction">

Abstract...

</chapter>

<chapter num="2" name="Body">

Abstract...

</chapter>

<chapter num="3" name="Conclusion">

Abstract...

</chapter>

</book>

</genre>

</Bookstore>

3)DataBinding\Data\Countries.xml

<?xml version="1.0"?>

<Countries>

<Country>

<Name>Argentina</Name>

</Country>

<Country>

<Name>Austria</Name>

</Country>

<Country>

<Name>Belgium</Name>

</Country>

<Country>

<Name>Brazil</Name>

</Country>

<Country>

<Name>Canada</Name>

</Country>

<Country>

<Name>Denmark</Name>

</Country>

<Country>

<Name>Finland</Name>

</Country>

<Country>

<Name>France</Name>

</Country>

<Country>

<Name>Germany</Name>

</Country>

<Country>

<Name>Ireland</Name>

</Country>

<Country>

<Name>Italy</Name>

</Country>

<Country>

<Name>Mexico</Name>

</Country>

<Country>

<Name>Norway</Name>

</Country>

<Country>

<Name>Poland</Name>

</Country>

<Country>

<Name>Portugal</Name>

</Country>

<Country>

<Name>Spain</Name>

</Country>

<Country>

<Name>Sweden</Name>

</Country>

<Country>

<Name>Switzerland</Name>

</Country>

<Country>

<Name>UK</Name>

</Country>

<Country>

<Name>USA</Name>

</Country>

<Country>

<Name>Venezuela</Name>

</Country>

</Countries>

4)DataBinding\CodeListing.txt

Dim stringConnect As String = ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString

Dim providerName As String = "System.Data.SqlClient"

Dim selectCommand As String = "SELECT ProductID, ProductName, UnitPrice FROM Products"

Dim sqlDS As New SqlDataSource(providerName, stringConnect, selectCommand)

GridView2.DataSource = sqlDS

GridView2.DataBind()

Label1.Text = sqlDS.SelectCommand.ToString()

5)DataBinding\DataSetDataSource.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Countries</title>

<link href="StyleSheet.css" type="text/css" rel="stylesheet" />

</head>

<body>

<form id="form1" runat="server">

<h2>Countries</h2>

<div>

  <asp:DataSetDataSource ID="DataSetDataSource1" Runat="server" DataFile="~/Data/Countries.xml"

DataMember="Country">

</asp:DataSetDataSource>

<asp:ListBox ID="ListBox1" Runat="server" Width="230px" Height="70px" DataSourceID="DataSetDataSource1"

DataTextField="Name">

</asp:ListBox>

</div>

</form>

</body>

</html>

6)DataBinding\Details.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

<link href="StyleSheet.css" type="text/css" rel="stylesheet" />

</head>

<body>

<form id="form1" runat="server">

<div>

Suppliers<br />

<br />

<asp:DetailsView ID="DetailsView1" Runat="server" BackColor="White" ForeColor="Black"

BorderWidth="1px" GridLines="Vertical" BorderColor="#DEDFDE" BorderStyle="None"

CellPadding="4" DataSourceID="SqlDataSource1" AutoGenerateRows="False" DataKeyNames="SupplierID">

<FooterStyle BackColor="#CCCC99"></FooterStyle>

<RowStyle BackColor="#F7F7DE"></RowStyle>

<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE"></PagerStyle>

<Fields>

<asp:BoundField ReadOnly="True" HeaderText="SupplierID" InsertVisible="False" DataField="SupplierID"

SortExpression="SupplierID"></asp:BoundField>

<asp:BoundField HeaderText="CompanyName" DataField="CompanyName" SortExpression="CompanyName"></asp:BoundField>

<asp:BoundField HeaderText="ContactName" DataField="ContactName" SortExpression="ContactName"></asp:BoundField>

<asp:BoundField HeaderText="ContactTitle" DataField="ContactTitle" SortExpression="ContactTitle"></asp:BoundField>

<asp:BoundField HeaderText="Address" DataField="Address" SortExpression="Address"></asp:BoundField>

<asp:BoundField HeaderText="City" DataField="City" SortExpression="City"></asp:BoundField>

<asp:BoundField HeaderText="Region" DataField="Region" SortExpression="Region"></asp:BoundField>

<asp:BoundField HeaderText="PostalCode" DataField="PostalCode" SortExpression="PostalCode"></asp:BoundField>

<asp:BoundField HeaderText="Country" DataField="Country" SortExpression="Country"></asp:BoundField>

<asp:BoundField HeaderText="Phone" DataField="Phone" SortExpression="Phone"></asp:BoundField>

</Fields>

<HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#6B696B"></HeaderStyle>

<EditRowStyle ForeColor="White" Font-Bold="True" BackColor="#CE5D5A"></EditRowStyle>

<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>

</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT SupplierID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone FROM dbo.Suppliers"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

<SelectParameters>

<asp:QueryStringParameter Name="SupplierID" QueryStringField="SupplierID"></asp:QueryStringParameter>

</SelectParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

7)DataBinding\ObjectDataSource.aspx

<%@ Page Language="VB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

<link href="StyleSheet.css" type="text/css" rel="stylesheet" />

</head>

<body>

<form id="form1" runat="server">

<div>

      

<asp:GridView ID="GridView1" Runat="server" DataSourceID="ObjectGenericCustomer" AutoGenerateColumns="False" BorderWidth="1px" BackColor="LightGoldenrodYellow" GridLines="None" CellPadding="2" BorderColor="Tan" ForeColor="Black">

<FooterStyle BackColor="Tan"></FooterStyle>

<PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"></PagerStyle>

<HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>

<AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>

<Columns>

<asp:BoundField ReadOnly="True" HeaderText="CustomerID" DataField="CustomerID" SortExpression="CustomerID"></asp:BoundField>

<asp:BoundField ReadOnly="True" HeaderText="CustomerName" DataField="CustomerName"

SortExpression="CustomerName"></asp:BoundField>

</Columns>

<SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle>

</asp:GridView>

<asp:ObjectDataSource ID="ObjectGenericCustomer" Runat="server" TypeName="CustomerList"

SelectMethod="GetCustomers">

</asp:ObjectDataSource><br />

 <asp:GridView ID="GridView2" Runat="server" DataSourceID="ObjectCustomerDB" BorderWidth="1px" BackColor="White" GridLines="Horizontal" CellPadding="3" BorderColor="#E7E7FF" BorderStyle="None">

<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>

<PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right" BackColor="#E7E7FF"></PagerStyle>

<HeaderStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#4A3C8C"></HeaderStyle>

<AlternatingRowStyle BackColor="#F7F7F7"></AlternatingRowStyle>

<SelectedRowStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#738A9C"></SelectedRowStyle>

<RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></RowStyle>

</asp:GridView> 

<asp:ObjectDataSource ID="ObjectCustomerDB" Runat="server" SelectMethod="GetCustomers"

TypeName="CustomerDB">

</asp:ObjectDataSource>

</div>

</form>

</body>

</html>

8)DataBinding\SqlDataSource.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

<link href="StyleSheet.css" type="text/css" rel="stylesheet" />

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = @original_CategoryID AND [CategoryName] = @original_CategoryName AND [Description] = @original_Description"

InsertCommand="INSERT INTO [Categories] ([CategoryName], [Description]) VALUES (@CategoryName, @Description)"

SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"

UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description] = @Description WHERE [CategoryID] = @original_CategoryID AND [CategoryName] = @original_CategoryName AND [Description] = @original_Description"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" ConflictDetection="CompareAllValues">

<DeleteParameters>

<asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter>

<asp:Parameter Type="String" Name="CategoryName"></asp:Parameter>

<asp:Parameter Type="String" Name="Description"></asp:Parameter>

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Type="String" Name="CategoryName"></asp:Parameter>

<asp:Parameter Type="String" Name="Description"></asp:Parameter>

<asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter>

</UpdateParameters>

<InsertParameters>

<asp:Parameter Type="String" Name="CategoryName"></asp:Parameter>

<asp:Parameter Type="String" Name="Description"></asp:Parameter>

</InsertParameters>

</asp:SqlDataSource> 

<asp:DropDownList ID="DropDownList1" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="CategoryName" DataValueField="CategoryID" AutoPostBack="True">

</asp:DropDownList> <br />

 <asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [UnitsInStock], [SupplierID], [CategoryID] FROM [Products] WHERE ([CategoryID] = @CategoryID)"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

<SelectParameters>

<asp:ControlParameter Name="CategoryID" Type="Int32" ControlID="DropDownList1" PropertyName="SelectedValue"></asp:ControlParameter>

</SelectParameters>

</asp:SqlDataSource>

<br />

  

<asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource2" DataKeyNames="ProductID"

AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True">

<Columns>

<asp:BoundField ReadOnly="True" HeaderText="ProductID" InsertVisible="False" DataField="ProductID"

SortExpression="ProductID"></asp:BoundField>

<asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName"></asp:BoundField>

<asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" SortExpression="UnitPrice"></asp:BoundField>

<asp:BoundField HeaderText="UnitsInStock" DataField="UnitsInStock" SortExpression="UnitsInStock"></asp:BoundField>

<asp:BoundField HeaderText="SupplierID" DataField="SupplierID" SortExpression="SupplierID"></asp:BoundField>

<asp:HyperLinkField Text="Supplier" DataNavigateUrlFields="SupplierID" DataNavigateUrlFormatString="Details.aspx?SupplierID={0}"></asp:HyperLinkField>

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>

9)DataBinding\StyleSheet.css

body

{

font-family: Verdana;

}

.Grid

{

font-size: 10pt;

font-family: Verdana;

}

H1

{

font-size: 24pt;

font-family: Verdana;

font-weight: bold;

}

.Button

{

font-size: 10pt;

font-family: Verdana;

}

INPUT

{

font-size: 10pt;

font-family: Verdana;

}

SELECT

{

font-size: 10pt;

font-family: Verdana;

}

10)DataBinding\Web.config

<?xml version="1.0" ?>

<!-- Note: As an alternative to hand editing this file you can use the web admin tool to

configure settings for your application. Use the Website->Asp.Net Configuration option

in Visual Studio.

A full list of settings and comments can be found in machine.config.comments usually

located in \Windows\Microsft.Net\Frameworks\v2.x\Config -->

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<appSettings />

<connectionStrings>

<add name="NorthwindConnection" connectionString="Server=localhost;Integrated Security=True;Database=Northwind;Persist Security Info=True"

providerName="System.Data.SqlClient" />

</connectionStrings>

<system.web>

<!--

Set compilation debug="true" to insert debugging symbols into the compiled page.

Because this affects performance, set this value to true only during development.

-->

<compilation debug="true" />

<!--

The <authentication> section enables configuration of the security authentication

mode used by ASP.NET to identify an incoming user.

-->

<authentication mode="Windows" />

<!--

The <customErrors> section enables configuration of what to do if/when an unhandled

error occurs during the execution of a request. Specifically, it enables developers

to configure html error pages to be displayed in place of a error stack trace.

-->

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<!--

<error statusCode="403" redirect="NoAccess.htm"/>

<error statusCode="404" redirect="FileNotFound.htm"/>

-->

</customErrors>

</system.web>

</configuration>

11)DataBinding\XMLDataSource.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

<link href="StyleSheet.css" type="text/css" rel="stylesheet" />

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="~/Data/Bookstore.xml"

XPath="Bookstore/genre[@name='Fiction']/book">

</asp:XmlDataSource>     

<asp:DataList ID="DataList1" Runat="server" DataSourceID="XmlDataSource1">

<ItemTemplate>

<table>

<tr>

<td>

<h4><%# Eval("Title") %></h4>

<b>ISBN:</b> <%# Eval("ISBN") %><br>

<b>Price:</b> <%# Eval("Price") %><br>

</td>

</tr>

</table>

</ItemTemplate>

</asp:DataList>

</div>

</form>

</body>

</html>


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
remote data storage
Color Laser Printer
Marc jacobs sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139