Ado Data Binding

21
Data Binding Controls ASP.NET comes with two sets of data aware control Data Source Control Data Bound Control

description

This is a ppt of ADO Data Controls information about aps.net

Transcript of Ado Data Binding

Page 1: Ado Data Binding

Data Binding Controls

• ASP.NET comes with two sets of data aware control

Data Source Control

Data Bound Control

Page 2: Ado Data Binding

DATA SOURCE CONTROLS

• These are used to bind data to the data bound control.

The XmlDataSource and SiteMapSource, are used to bind hierarchical, XML-based data to these controls.

The AccessDataSource is used to display data from a Microsoft Access database in your web pages.

The ObjectDataSource allows you to connect your data-bound controls to separate objects in your application.

Page 3: Ado Data Binding

DATA BOUND CONTROL

• These are used to display and edit data.

• Data bound control can be divided in two types:-

Simple Data Bound ControlComplex Data Bound Control

These are designed to show one record at a time. These are:-

DetailsView FormView

These are designed to show multiple records at the same time. These are:-

GridViewDataListListViewRepeaters

Page 4: Ado Data Binding

SQL Data Source• Provides access to any data source that has an ADO.NET Data Provider available; by default, the control has access to the ODBC, OLE DB, SQL Server, Oracle, and SQL Server CE providers.

• The steps that we follow to configure our SQL data source are as follows:-

1. Drag and drop sqldatasource control from the tool box on the web page.

Page 5: Ado Data Binding

SQL Data Source(Cont.)

2. Then configure your sqldatasource control:-

Click here

Page 6: Ado Data Binding

SQL Data Source(Cont.)Click Here

Type the sqlserver name here

Select your database

Page 7: Ado Data Binding

SQL Data Source(Cont.)

Connection string

Page 8: Ado Data Binding

Storing Connection Information

• web.config file is a central location for storing application configuration data in a readable and portable format.

<connectionStrings><add name="AppCon" connectionString="Server=localhost;User ID=sa;Password=password;Database=Northwind;Persist Security Info=True" providerName="System.Data.SqlClient" /></connectionStrings>

Note:- To retrieve connection string from web.config file we use-String constr= System.Configuration.ConfigurationManager.ConnectionStrings[“AppCon"].ConnectionString;

Page 9: Ado Data Binding

Data Bound ControlRepeater:-•The Repeater control is used to display a repeated list of items that are bound to the control.•The Repeater control may be bound to a database table, an XML file, or another list of items. •It has no built-in layout or styles, so you must explicitly declare all layout, formatting, and style tags within the control's templates. •Repeater has 5 inline template to format it:1. <HeaderTemplate>

2. <FooterTemplate>

3. <ItemTemplate>

4. <AlternatingItemTemplate>

5. <SeperatorTemplate>

This template is used for elements that you want to render once before your ItemTemplate section.

This template is used for elements that you want to render once after your ItemTemplate section.

This template is used for elements that are rendered once per row of data. It is used to display records

This template is used for elements that are rendered every second row of data

It is used for elements to render between each row, such as line breaks.

Page 10: Ado Data Binding

Data Bound Control(Cont.)using System;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Wb.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page{ SqlConnection con; SqlCommand cmd = new SqlCommand(); protected void Page_Load(object sender, EventArgs e) { con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString); cmd.Connection = con; cmd.CommandText = "select * from student"; con.Open(); RepeatInformation.DataSource = cmd.ExecuteReader(); RepeatInformation.DataBind(); con.Close(); }}

Page 11: Ado Data Binding

Data Bound Control(Cont.)<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <link rel="Stylesheet" type="text/css" href="StyleSheet.css" /> <title>Repeater Controls in ASP.NET</title></head><body> <form id="form1" runat="server"> <div> <asp:Repeater ID="RepeatInformation" runat="server"> <HeaderTemplate><table class="tblcolor“><tr> <b><td>Roll No</td><td>Student Name </td><td>Total Fees</td></b></tr></HeaderTemplate> <ItemTemplate> <tr class="tblrowcolor“> <td> <%#DataBinder.Eval(Container,"DataItem.RollNo")%> </td><td> <%#DataBinder.Eval(Container,"DataItem.Name")%> </td> <td> <%#DataBinder.Eval(Container,"DataItem.Fees")%></td> </tr></ItemTemplate> <SeparatorTemplate> <tr><td><hr /></td><td><hr /></td> <td><hr /> </td> </tr></SeparatorTemplate> <AlternatingItemTemplate><tr><td> <%#DataBinder.Eval(Container,"DataItem.RollNo")%> </td> <td> <%#DataBinder.Eval(Container,"DataItem.Name")%></td> <td> <%#DataBinder.Eval(Container,"DataItem.Fees")%></td></tr> </AlternatingItemTemplate> <SeparatorTemplate> <tr> <td> <hr /> </td> <td> <hr /> </td> <td> <hr /> </td> </tr> </SeparatorTemplate> <FooterTemplate> <tr> <td> School Records displayed </td></tr> </table> </FooterTemplate> </asp:Repeater> </div> </form></body></html>

Page 12: Ado Data Binding

Data Bound Control(Cont.)

The Output of this example:-

Page 13: Ado Data Binding

Data Bound Control(Cont.)

GridView:-The GridView control is used to display the values of a data source in a table. Each column represents a field, while each row represents a recor

• Steps for using GridView:-

Binding to data source controls, such as SqlDataSource

Page 14: Ado Data Binding

Data Bound Control(Cont.)

AutoFormat:-

Paging ,Sorting and Selection:-

Page 15: Ado Data Binding

Data Bound Control(Cont.)

Editing , Deleting and Selection:-

OutPut:-

Page 16: Ado Data Binding

Data Bound Control(Cont.)Data List:-

Data binding:- By using DataSourceId property we bind the Data List with a particular data source.

The DataList control is, like the Repeater control, used to display a repeated list of items that are bound to the control.

Templates :-We have different templates in Data List through which we can configure the appearance of Data List and also the functionality of this control.

Page 17: Ado Data Binding

Data Bound Control(Cont.)

List View:-•The ASP.NET ListView control enables you to bind to data items that are returned from a data source and display them. You can display data in pages

• It is useful for data in any repeating structure, similar to the DataList and Repeater controls. However, unlike those controls, with the ListView control you can enable users to edit, insert, and delete data, and to sort and page data, all without code.

Binding Data Source:- Configuring List View:-

Page 18: Ado Data Binding

Data Bound Control(Cont.)

Designing List View:-

Output:-

Page 19: Ado Data Binding

Data Bound Control(Cont.)

Details View:- The DetailsView control in ASP.Net 4.0 is used to create an HTML table that displays the contents of a single database record.

Binding DataBase:-

Paging, Inserting, Editing and Deleting:-

Page 20: Ado Data Binding

OutPut:-

Data Bound Control(Cont.)

Page 21: Ado Data Binding

Data Bound Control(Cont.)