Monday, July 23, 2012

DataView:
A DataView defines a view onto a DataTable object. It is a representation of a data in a DataTable that can include custom filtering and sorting. The Data Views are particularly useful in data binding scenarios.

            //Windows Authentication
            String ConnectionString = "server=localhost; database=AdventureWorks; Integrated Security=True";

            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();

            string qryString = "SELECT TOP 10 * FROM Person.Contact";
            SqlDataAdapter da = new SqlDataAdapter(qryString, conn);

            DataSet ds = new DataSet();
            da.Fill(ds, "Employee");
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "FirstName='Govson'";
            dv.Sort = "contactID ASC";
   
Overview of Grid Controls:

DataRepeter Control:
 This control enables to display set of items from data source with any type of formatting. It can be bind a Repeater control to either a DataSet or a Data Reader. While using Repeater control we are required to supply at least one template.
Repeater Control supports the following templates:
  • Item Template
  • Alternating Item Template
  • Header Template
  • Separator Template
  • Footer Template


Working of a DataAdapter:



        protected void Page_Load(object sender, EventArgs e)
        {
            //Windows Authentication
            String ConnectionString = "server=localhost; database=AdventureWorks; Integrated Security=True";

            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();

            string qryString = "SELECT TOP 10 * FROM Person.Contact";
            SqlDataAdapter da = new SqlDataAdapter(qryString, conn);

            DataSet ds = new DataSet();
            da.Fill(ds, "Employee");


        }
DataSet Component:

A DataSet is the component responsible for disconnected data architecture.  Every DataTable of a DataSet contains Columns collection of DataColumn object and the Rows collection of DataRow objects.
The DataRow contains actual field values and these field values can be accessed field name, like inmyRow[“FiledName”].

The data in the data source is never touched at all when you are working with the DataSet objects. Instead, all the changes are done locally to the DataSet in memory. Then the changes which are made into the DataSet will be updated in the actual database.

ADO.NET Providers:

A data provider is nothing but a set of ADO.NET classes that can be used to access a specific database and execute commands against it and retrieve the data. Mainly, data provider acts like a bridge between your application and data source.

When you try to choose a provider, you should first try to find a native .NET provider that is customized for your data source.

ADO.NET Architecture:

As we have discussed previously the data provider includes the following components or classes
Connection: Object to establish connection with a data source(SQL, OLEDB, Oracle etc.)
Command: Object is responsible for SQL commands and stored procedures
DataReader: Object is responsible for providing fast, read-only, forward-only access to the data retrieved by executing a query.
Data Adapter: Object to fill a DataSet with the data retrieved from data source.  It uses to apply the changes after modifying the data within a dataset. And it play key role in the disconnected data architecture.

ADO.NET Classes:

DataProvider:


Connection
Provides Connectivity to data source
Command
Runs queries and executes other database commands
DataReader
Providing-High Performance read-access data
DataAdapter
Loads data into DataSets and applies changes

Data Table
Contains rows and columns of data from a query
Command
Stores relationship between Data tables


DataSet:
Connection Object:


Its allows you to establish a connection with a data source. The connection class implements the interface known as IDbInterface, which consists of all the core connection properties and methods. Mainly these connection settings are included with the server where database is located, the database you want to use and database authentication credentials.
Connection object mainly supports
Properties: ConnectionString, ConnectionTimeout
Methods: open (), close()
Syntax:
//Windows Authentication
String ConnectionString = "server=localhost; database=AdventureWorks; Integrated Security=True";

            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();

//If you provide username and password, SQL Authentication
String ConnectionString = "server=localhost; database=AdventureWorks; UserID=ramakrishna; Password=sysadmin";

NOTE: It is recommended to place connection string in web.config file in your application to avoid security threats.

Command Object:

The command class allows you to execute any type of SQL statement against a database. The command object is mainly used to perform data manipulation tasks, such as retrieving and updating the records in a table. It implements, IDbCommand interface defines a few core properties & methods to execute a command over an open connection to the database.
Command Object:
Properties supported:
·        CommandType
·        CommandText
·        Connection
·        Parameters
Methods supported:
·        ExecuteNonQuery()
·        ExecuteReader()
·        ExecuteScalar()
Once created, the command object must be configured by setting the Connection, CommandType, and CommandText properties.

DataReader Object:

A DataReader class allows you to read the data returned by a SELECT command one record at a time, in forward-only, read-only stream.

Results are returned as the query executes, and are stored in the network buffer on the client. DataReader increases application performance.

Always call the Close() method when you have finished using the DataReader object.


DataAdapter:

Data adapters are an integral part of ADO.NET managed providers, which are the set of objects used to communicate between a datasource and a dataset.

Data adapters retrieve data from database into DataSet and update the changes made within the dataset back to the database.

DataAdapter provides three key methods:
·        Fill()
o   Method adds a DataTable to a DataSet by executing the query in the SelectCommand.  If your query returns multiple result sets, this method will add multiple DataTable objects to the DataSet.
·        FillSchema()
o   Method adds a DataTable to a DataSet by executing the query in the SelectCommand and retrieving schema information only.
·        Update()
o   Method examines all the changes in a single DataTable and applies this batch of changes to the actual data source by executing the appropriate commands.

ASP .NET 4.0 :

ASP.NET is a Web platform that provides all the services that you require to build enterprise-class server-based Web applications. ASP.NET is built on the .NET Framework, so all .NET Framework features are available to ASP.NET applications.

Your applications can be written in any language that is compatible with the common language runtime (CLR), including Visual Basic and C#.
To create ASP.NET Web applications, you can use Visual Studio. The tools and options in Visual Studio that are designed for creating Web applications are referred to collectively as Visual Web Developer. In addition, a free standalone product—Visual Web Developer Express—is available that includes the core set of Web-design features from Visual Studio.
The following topics provide more information about ASP.NET and Visual Web Developer.

 


What is ADO.NET


ADO.NET is the data access technology built within the .NET framework.


ADO.NET consists of managed classes that allow .NET applications to connect to data sources, execute commands, and manage disconnected data.


ADO.NET uses a multilayered architecture that is basically comprised of components such as Connection, Command, DataReader, and Dataset objects.


ADO.NET Architecture:
ADO.NET Architecture Diagram
ADO.NET addresses rich set of components which are responsible for creating distributed and data sharing applications. It is tightly integrated with the .NET framework with an accessibility to relational, XML, and application data.


ADO.NET also addresses a variety of development needs like the creation of front-end database clients and middle-tier business objects used by applications, tools, languages, or Internet browsers.
Introduction to Data binding


Data binding helps to develop applications where controls can be bound to the values stored in a data source.

Web server controls can be bounded to data source such as an array list, database table dynamically.

The properties of a server control are set to a specific data source and when the application executes, it fetches data from the data source and displays it.

Data bind by using the following syntax:

    <asp:TextBox ID="txtName" Text="Enter Name" runat="server">Enter Name</asp:TextBox>
    <asp:Label ID="lblName" Text="<% #txtName.Text%>" runat="server"/>

The sever control will display the data when the DataBind() method is called on that control.

protected void Page_Load(object sender, EventArgs e)
        {
if (IsPostBack)
{
lblName.DataBind();
}

}

Types of Data binding:

There are two types of data binding available in ASP.NET
  •  Single-value data binding
    •  Displays the single line of data value at a time from the data source Eg: TextBox and Label Controls
  • Multiple values data binding 
    • Displays the multi line of data values at a time from the data source Eg: DropDownlist, ArrayList, GridView, ListView etc.


 Eg: Multiple Databinding

protected void Page_Load(object sender, EventArgs e)
        {

            ArrayList arry = new ArrayList();
            arry.Add("First");
            arry.Add("Second");
            arry.Add("Third");

            DropDown1.DataSource = arry;
            DropDown1.DataBind();
       }

Monday, July 9, 2012

How to bind data to a list control:

List control class is avaliaable in  System.Web.UI.WebControls. It servers as the abstract base class that defines the properties, methods, and events common for all list-type controls.

Syntax in ASP.Net:             <asp:ListControl />

Sample Example to bind List control data from a Stored Procedure:

Sample.aspx page: