Monday, 12 December 2011

About Data Source Controls

Data Source Controls provides a way of  populating controls with data declaratively,
Controls that support data Binding  have a property and  a method
1) Data Source Property (May be populated by array,DataReader, DataSet (Collections of data such as  an array or DataSet))
2) DataBind() method.

Server Controls support data binding , including simple controls, such as

1)   ListBox
2)  GridView
3 ) DetailsView and ect.


Declarative data binding with sqlDataSource example
<asp:GridView ID="123Gridview" runat="server" DataSourceID="123DataSource"/>
<asp:SqlDataSource ID="123DataSource" runat ="server"
   DataSourcePublish PostMode="DataReader"
 ConnectionString="DataBase="pubs;trusted_connection=yes"
 SelectCommand="Select *from Authors"/>


Imperative Data Binding

<%@page Language="C#" %>
<% import Namespace="System.Data.SqlClient" %>

<script  runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
        {
       using (SqlConnection con= new SqlConnection("database=pubs;trusted_connection=yes"))
       using (SqlCommand cmd= new SqlCommand("select * from authors",con))

          {
   con.Open();
   SqlDataReader reader=cmd.ExecuteReader();
  123GridView.DataSource=reader;
 123GridView.DataBind();
        }
}
</script>
<html>
<head runat="server">
<title>ImperativeDataBinding</title>
</head>
<body>
<form id="f1" runat="server">
<div> <asp:Gridview runat="server  id="123GridView"  /> </div>
</form>
</body>


The above code show  Gridview bound to  atuthors table in pubs databse in swql uses a Data Reader to retrieve the data .



No comments:

Post a Comment