ASP .net material Part-7


ASP.NET - Database Connection

ADO.NET is also a part of the .NET Framework. ADO.NET is used to handle data access. With ADO.NET you can work with databases.

What is ADO.NET?

  • ADO.NET is a part of the .NET Framework
  • ADO.NET consists of a set of classes used to handle data access
  • ADO.NET is entirely based on XML
  • ADO.NET has, unlike ADO, no Recordset object

Create a Database Connection

We are going to use the Northwind database in our examples.
First, import the "System.Data.OleDb" namespace. We need this namespace to work with Microsoft Access and other OLE DB database providers. We will create the connection to the database in the Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class with a connection string which identifies the OLE DB provider and the location of the database. Then we open the database connection:
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

sub Page_Load
dim dbconn
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
end sub
</script>
Note: The connection string must be a continuous string without a line break!

Create a Database Command

To specify the records to retrieve from the database, we will create a dbcomm variable as a new OleDbCommand class. The OleDbCommand class is for issuing SQL queries against database tables:
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

sub Page_Load
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
end sub
</script>


Create a DataReader

The OleDbDataReader class is used to read a stream of records from a data source. A DataReader is created by calling the ExecuteReader method of the OleDbCommand object:
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
</script>


Bind to a Repeater Control

Then we bind the DataReader to a Repeater control:

Example

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

<script runat="server">

sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>

<body>

<form runat="server">

<asp:Repeater id="customers" runat="server">

<HeaderTemplate>

<table border="1" width="100%">
<tr>
<th>Companyname</th>
<th>Contactname</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>

<ItemTemplate>

<tr>
<td><%#Container.DataItem("companyname")%></td>
<td><%#Container.DataItem("contactname")%></td>
<td><%#Container.DataItem("address")%></td>
<td><%#Container.DataItem("city")%></td>
</tr>
</ItemTemplate>

<FooterTemplate>

</table>
</FooterTemplate>

</asp:Repeater>

</form>

</body>

</html>

Close the Database Connection

Always close both the DataReader and database connection after access to the database is no longer required:
dbread.Close()
dbconn.Close()

ASP.NET 2.0 - New Features

ASP.NET 2.0 improves ASP.NET by adding several new features.

Improvements in ASP.NET 2.0

ASP.NET 2.0 was designed to make web development easier and quicker.
Design goals for ASP.NET 2.0:
  • Increase productivity by removing 70% of the code
  • Use the same controls for all types of devices
  • Provide a faster and better web server platform
  • Simplify compilation and installation
  • Simplify the administration of web applications

What's New in ASP.NET 2.0?

Some of the new features in ASP.NET 2.0 are:
  • Master Pages, Themes, and Web Parts
  • Standard controls for navigation
  • Standard controls for security
  • Roles, personalization, and internationalization services
  • Improved and simplified data access controls
  • Full support for XML standards like, XHTML, XML, and WSDL
  • Improved compilation and deployment (installation)
  • Improved site management
  • New and improved development tools
The new features are described below.

Master Pages

ASP.NET didn't have a method for applying a consistent look and feel for a whole web site.
Master pages in ASP.NET 2.0 solves this problem.
A master page is a template for other pages, with shared layout and functionality. The master page defines placeholders for content pages. The result page is a combination (merge) of the master page and the content page.
Read more about master pages.

Themes

Themes is another feature of ASP.NET 2.0. Themes, or skins, allow developers to create a customized look for web applications.
Design goals for ASP.NET 2.0 themes:
  • Make it simple to customize the appearance of a site
  • Allow themes to be applied to controls, pages, and entire sites
  • Allow all visual elements to be customized

Web Parts

ASP.NET 2.0 Web Parts can provide a consistent look for a site, while still allowing user customization of style and content.
New controls:
  • Zone controls - areas on a page where the content is consistent
  • Web part controls - content areas for each zone

Navigation

ASP.NET 2.0 has built-in navigation controls like
  • Site Maps
  • Dynamic HTML menus
  • Tree Views  

Security

Security is very important for protecting confidential and personal information.
In ASP.NET 2.0 the following controls has been added:
  • A Login control, which provides login functionality
  • A LoginStatus control, to control the login status
  • A LoginName control to display the current user name
  • A LoginView control, to provide different views depending on login status
  • A CreateUser wizard, to allow creation of user accounts
  • A PasswordRecovery control, to provide the "I forgot my password" functionality

Roles and Personalization

Internet communities are growing very popular.
ASP.NET 2.0 has personalization features for storing user details. This provides an easy way to customize user (and user group) properties.

Internationalization

Reaching people with different languages is important if you want to reach a larger audience.
ASP.NET 2.0 has improved support for multiple languages.

Data Access

Many web sites are data driven, using databases or XML files as data sources.
With ASP.NET this involved code, and often the same code had to be used over and over in different web pages.
A key goal of ASP.NET 2.0 was to ease the use of data sources.
ASP.NET 2.0 has new data controls, removing much of the need for programming and in-depth knowledge of data connections.

Mobility Support

The problem with Mobile devices is screen size and display capabilities.
In ASP.NET, the Microsoft Mobile Internet Toolkit (MMIT) provided this support.
In ASP.NET 2.0, MMIT is no longer needed because mobile support is built into all controls.

Images

ASP.NET 2.0 has new controls for handling images:
  • The ImageMap control - image map support
  • The DynamicImage control  - image support for different browsers
These controls are important for better image display on mobile devices, like hand-held computers and cell phones.

Automatic Compilation

ASP.NET 2.0 provides automatic compilation. All files within a directory will be compiled on the first run, including support for WSDL, and XSD files.

Compiled Deployment (Installation) and Source Protection

ASP.NET 2.0 also provides pre-compilation. An entire web site can be pre-compiled. This provides an easy way to deploy (upload to a server) compiled applications, and because only compiled files are deployed, the source code is protected.

Site Management

ASP.NET 2.0 has three new features for web site configuration and management:
  • New local management console
  • New programmable management functions (API)
  • New web-based management tool

Development Tools

With ASP.NET Visual Studio.NET was released with project and design features targeted at corporate developers.
With ASP.NET 2.0, Visual Studio 2005 was released.
Key design features for Visual Studio 2005 include:
  • Support for the features described above
  • Upload files from anywhere (FTP, File System, Front Page....)
  • No project files, allowing code to be manipulated outside Visual Studio
  • Integrated Web Site Administration Tool
  • No "build" step - ability to compile on first run
Visual Web Developer is a new free ASP.NET 2.0 tool for non-corporate developers who don't have access to Visual Studio.NET.

ASP.NET 2.0 - Master Pages

Master pages provide templates for other pages on your web site.

Master Pages

Master pages allow you to create a consistent look and behavior for all the pages (or group of pages) in your web application.
A master page provides a template for other pages, with shared layout and functionality. The master page defines placeholders for the content, which can be overridden by content pages. The output result is a combination of the master page and the content page.
The content pages contains the content you want to display.
When users request the content page, ASP.NET merges the pages to produce output that combines the layout of the master page with the content of the content page.

Master Page Example

<%@ Master %>

<html>

<body>
<h1>Standard Header For All Pages</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
The master page above is a normal HTML page designed as a template for other pages.
The @ Master directive defines it as a master page.
The master page contains a placeholder tag <asp:ContentPlaceHolder> for individual content.
The id="CPH1" attribute identifies the placeholder, allowing many placeholders in the same master page.
This master page was saved with the name "master1.master".
lampNote: The master page can also contain code, allowing dynamic content.

Content Page Example

<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">

  <h2>Individual Content</h2>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
</asp:Content>
The content page above is one of the individual content pages of the web.
The @ Page directive defines it as a standard content page.
The content page contains a content tag <asp:Content> with a reference to the master page (ContentPlaceHolderId="CPH1").
This content page was saved with the name "mypage1.aspx".
When the user requests this page, ASP.NET merges the content page with the master page.
Click to display mypage1.aspx.
lampNote: The content text must be inside the <asp:Content> tag. No content is allowed outside the tag.

Content Page With Controls

<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">

  <h2>W3Schools</h2>
  <form runat="server">
    <asp:TextBox id="textbox1" runat="server" />
    <asp:Button id="button1" runat="server" text="Button" />
  </form>
</asp:Content>
The content page above demonstrates how .NET controls can be inserted into the content page just like an into an ordinary page.

ASP.NET 2.0 - Navigation

ASP.NET 2.0 has built-in navigation controls

Web Site Navigation

Maintaining the menu of a large web site is difficult and time consuming.
In ASP.NET 2.0 the menu can be stored in a file to make it easier to maintain. This file is normally called web.sitemap, and is stored in the root directory of the web.
In addition, ASP.NET 2.0 has three new navigation controls:
  • Dynamic menus
  • TreeViews
  • Site Map Path

The Sitemap File

The following sitemap file is used in this tutorial:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<siteMap>
  <siteMapNode title="Home" url="/aspnet/w3home.aspx">
    <siteMapNode title="Services" url="/aspnet/w3services.aspx">
      <siteMapNode title="Training" url="/aspnet/w3training.aspx"/>
      <siteMapNode title="Support" url="/aspnet/w3support.aspx"/>
    </siteMapNode>
  </siteMapNode>
</siteMap>
Rules for creating a sitemap file:
  • The XML file must contain a <siteMap> tag surrounding the content
  • The <siteMap> tag can only have one <siteMapNode> child node (the "home" page)
  • Each <siteMapNode> can have several child nodes (web pages)
  • Each <siteMapNode> has attributes defining page title and URL
lampNote: The sitemap file must be placed in the root directory of the web and the URL attributes must be relative to the root directory.

Dynamic Menu

The <asp:Menu> control displays a standard site navigation menu.
Code Example:
<asp:SiteMapDataSource id="nav1" runat="server" />

<form runat="server">

<asp:Menu runat="server" DataSourceId="nav1" />
</form>
The <asp:Menu> control in the example above is a placeholder for a server created navigation menu.
The data source of the control is defined by the DataSourceId attribute. The id="nav1" connects it to the  <asp:SiteMapDataSource> control.
The <asp:SiteMapDataSource> control automatically connects to the default sitemap file (web.sitemap).
Click here to see a demo of Menu, TreeView, and SiteMapPath

TreeView

The <asp:TreeView> control displays a multi level navigation menu.
The menu looks like a tree with branches that can be opened or closed with + or - symbol.
Code Example:
<asp:SiteMapDataSource id="nav1" runat="server" />

<form runat="server">

<asp:TreeView runat="server" DataSourceId="nav1" />
</form>
The <asp:TreeView> control in the example above is a placeholder for a server created navigation menu.
The data source of the control is defined by the DataSourceId attribute. The id="nav1" connects it to the  <asp:SiteMapDataSource> control.
The <asp:SiteMapDataSource> control automatically connects to the default sitemap file (web.sitemap).

SiteMapPath

The SiteMapPath control displays the trail (navigation path) to the current page. The path acts as clickable links to previous pages.
Unlike the TreeView and Menu control the SiteMapPath control does NOT use a SiteMapDataSource. The SiteMapPath control uses the web.sitemap file by default.
lampTips: If the SiteMapPath displays incorrectly, most likely there is an URL error (typo) in the web.sitemap file.
Code Example:
<form runat="server">
<asp:SiteMapPath runat="server" />
</form>
The <asp:SiteMapPath> control in the example above is a placeholder for a server created site path display.



Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender