In this blog you will find ASP.NET 3.5 articles and some code samples. These code samples are created by me and should be working. Thanks for visiting my blog! Happy Dot Netting

Tuesday, September 23, 2008

Data bound Controls

You use DataBound controls to generate your application’s user interface for working with data. The dataBound controls can be used to display and edit database data, XML data, or just about any other type of data you can imagine. There are three main types of DataBound controls: list controls, tabular DataBound controls, and hierarchical DataBound controls.

Working with List Controls
List controls are used to display simple option lists. The ASP.NET 3.5 Framework includes the following five List controls:

BulletedList— displays a bulleted list of items. Each item can be displayed as text, a link button, or a hyperlink.
CheckBoxList—displays a list of check boxes. Multiple check boxes in the list can be selected.
DropDownList—displays a drop-down list. Only one item in the drop-down list can be selected.
ListBox—displays a list box. You can configure this control so that only one item in the list can be selected or multiple items can be selected.
RadioButtonList—displays a list of radio buttons. Only one radio button can be selected.

Working with Tabular DataBound Controls
The tabular DataBound controls are the main set of controls that you use when working with database data. These controls enable you to display and, in some cases, modify data retrieved from a database or other type of data source.
There are six tabular DataBound controls. These controls can be divided into two types: Those that display multiple data items at a time and those that display a single data item at a time.
First, you can use any of the following controls to display a set of data items:
 
GridView—Displays a set of data items in an HTML table. For example, you can use the GridView control to display all the records contained in the Movies database table. This control enables you to display, sort, page, select, and edit data.
DataList—Displays a set of data items in an HTML table. Unlike the GridView control, more than one data item can be displayed in a single row.
Repeater—Displays a set of data items using a template. Unlike the GridView and DataList controls, a Repeater control does not automatically render an HTML table.
ListView—Displays a set of data items using a template. Unlike the Repeater control, the ListView control supports sorting, paging, and editing database data. You can use either of the following two controls to display a single data item at a time:
DetailsView—displays a single data item in an HTML table. For example, you can use the DetailsView control to display a single record from the Movies database table. This control enables you to display, page, edit, and add data.
FormView—Uses a template to display a single data item. Unlike the DetailsView, a FormView enables you to layout a form by using templates.

Working with Hierarchical DataBound Controls
A hierarchical DataBound control can be used to display nested data items. For example, you can use hierarchical DataBound controls to display the folder and page structure of your website, the contents of an XML file, or a set of master/detail database records. The ASP.NET 3.5 Framework includes two hierarchical DataBound controls:

Menu—Displays data items in a static or dynamic menu.
TreeView—Displays data items in a tree.

Using Data Source control
You bind a DataBound control to a DataSource control. A DataSource control is used to represent a particular type of data. The ASP.NET 3.5 Framework includes the following six DataSource controls:

SqlDataSource—Represents data retrieved from a SQL relational database, including Microsoft SQL  server, Oracle, or DB2.
LinqDataSource—Represents a LINQ to SQL query.
AccessDataSource—Represents data retrieved from a Microsoft Access database.
ObjectDataSource—Represents data retrieved from a business object.
XmlDataSource—Represents data retrieved from an XML document.
siteMapDataSource—Represents data retrieved from a Site Map Provider. A Site Map Provider represents the page and folder structure of a website.

The ASP.NET Framework contains two basic types of DataSource controls. The SqlDataSource, AccessDataSource, LinqDataSource, and ObjectDataSource controls all derive from the base DataSourceControl class. These controls can be used to represent tabular data. The XmlDataSource and SiteMapDataSource controls, on the other hand, derive from the base HierarchicalDataSourceControl control. These two controls can be used to represent both tabular and hierarchical data.

Programmatic data binding
Every DataBound control has a DataSource property and a DataBind() method. By using this property and method, you can programmatically associate a DataBound control with a data source.

Understanding Templates and DataBinding Expressions
Every DataBound control included in the ASP.NET 3.5 Framework supports templates with the sole exception of the TreeView control. You can use a template to format the layout and appearance of each of the data items that a DataBound control displays
A DataBinding expression is a special type of expression that is not evaluated until runtime. You mark a Databinding expression in a page by wrapping the expression in opening brackets.

< %# Eval(“Title”) %  >
< %# Eval(“Director”) % >

Thursday, September 11, 2008

SQLDataSource Control


1. The SqlDataSource control is built on top of ADO.NET. Under the covers, the SqlDataSource uses ADO.NET objectssuch as the DataSet, DataReader, and Command objects.

2. The SqlDataSource control is not an appropriate control to use when building more complicated multi-tier applications. The SqlDataSource control forces you to mix your data access layer with your user interface layer. If you want to build a more cleanly architected multi-tier application, then you should use the ObjectDataSource controlto represent your database data.

3. Accessing through connection String  
asp:SqlDataSource id="srcMovies" SelectCommand="SELECT * FROM Movies"  ConnectionString="" Runat="server" /
The expression is used to represent the connection string.

4. The SqlDataSource control can represent the data that it retrieves in two different ways. It can represent the data using either an ADO.NET DataSet or an ADO.NET DataReader.The advantage of using a DataReader is that it offers significantly better performance than the DataSet object.

5. Handling SQL Command Execution Errors

Whenever you build a software application you need to plan for failure. Databases go down, users enter unexpected values in form fields, and networks get clogged. It is miraculous that the Internet works at all. You can handle errors thrown by the SqlDataSource control by handling any or all of the following four events:

Deleted—Happens immediately after the SqlDataSource executes its delete command.
Inserted—Happens immediately after the SqlDataSource executes its insert command.
Selected—Happens immediately after the SqlDataSource executes its select command.
Updated—Happens immediately after the SqlDataSource executes its delete command.


protected void srcMovies_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = e.Exception.Message;
e.ExceptionHandled = true;
}
}

6. Canceling Command Execution

You can cancel SqlDataSource commands when some criterion is not met. For example, you might want to validate the parameters that you are using with the command before executing the command. You can cancel a command by handling any of the following events exposed by the SqlDataSource control:

Deleting—Happens immediately before the SqlDataSource executes its delete command.
Filtering—Happens immediately before the SqlDataSource filters its data.
Inserting—Happens immediately before the SqlDataSource executes its insert command.
Selecting—Happens immediately before the SqlDataSource executes its select command.
Updating—Happens immediately before the SqlDataSource executes its delete command.