Data Access

There are 3 main types of dataBound controls
- List Controls
- Tabular databound controls
- Hierarchical databound controls
List Controls:
- BulletedList
- CheckboxList
- DropDownList
- Listbox
- RadioButtonList
Tabular DataBound Controls
- Display a set of data
- GridView
- DataList
- Repeater
- Display a single data item at a time
- DetailsView
- FormView
The DataGrid is included in ASP.Net 2.0 for backward compatibility. It is not recommended to use it
Hierarchical DataBound Controls
- Menu
- TreeView
Both of these controls are bound to an XMLDataSource Control
You can bind any control to these data items
You can also bind any control to a data item by adding the control to a template

DataSource Controls
- SQLDataSource
- Retrieve data from a SQL relational database
- AccessDataSOurce
- Retrieve from a Microsoft Access database
- ObjectDataSource
- Retrieve data from a business object
- XMLDataSource
- Retrieve data from an XML document
- SiteMapDataSOurce
- Data retrieved from a sitemap provider
These fall within one of two categories
Represent tabular data:
- SQLDataSource
- AccessDataSource
- ObjectDataSource
Represent tabular and hierarchical data
- XMLDataSource
- SiteMapDataSource
Databound controls are associated with one of these dataSources with its DataSourceID property

DataSource Controls and Parameters
SQLDataSource, AccessdataSOurce and ObjectDataSOurce can use the following parameters:
- Paremeter
- ControlParameter
- Value of a control or page property
- CookieParameter
- Value of a browser cookie
- FormParameter
- Value of an HTML form field
- ProfileParameter
- Value of a profile property
- QueryStringParameter
- Value of a querystring field
- SessionParameter
- Value of an item stored in a session
A parameter in SQLDataSource represents an ADO.Net parameter
A parameter in ObjectDataSource represents a method parameter

This example allows the user to select a record from a dropDownList and displays the details in a gridView. The second data source control uses a controlparameter in the where clause |
- Download the following file
http://www.prowebdesigners.com/aspnet/resources/database.mdf
- Open Visual Studio
- Add a new webform
- Add an existing item, select the database file you just downloaded
- Add it to the App_Data directory

- Add a SQLDataSource control to the page(from the data tab in the toolbox)

- Click the smart tag and select Configure Data Source
- From the dropdown list select the database.mdf file
- Visual Studio will recommend saving your connection string in your configuration file. This is a very good practice. Click Next.

- Select your table and the columns you want. Click * to select all columns.
- Click OrderBy to sort the data
- Select the column you want to sort by in the dropdown list


- Click Test Query to test it
- Click Finish

Here is the code:


- Add a dropDownList Control
- Select Choose Data Source

- Select the SQLDataSource you just set up

Select the field you want to display in the dropDownList then the field you want to pass when submitted.
Select SurveyName to display
Select Survey_id to pass when submitted. This will uniquely identify the selected row

- Click OK
- Set the AutoPostBack property to True
- Test it
Here is the code:


- Add another SQLDataSource control
- Select Configure Data Source

- Select the connection string you created in the last data Source

- Select all columns and then click the Where button

- Select Survey_Id as the column
- Make sure = is selected in the second dropdownList
- Select control as the Source
This will retrieve the parameter from a control. Once you select this more options appear
- Select the dropDownList control
- Click Add
It adds the code for your parameter

You where clause is now complete


Here is the code:


- Add a gridView control
- Click Choose Data Source

- Select the second dataSource you just created

When you select a name from the dropDownList the record displays in the gridView
Here is the code for the gridView:


Programmatic dataBinding
ASP.Net 1.1 only supported this type of dataBinding
Example:
Dim fonts as New InstalledFontCollection()
controlName.dataSource=fonts.families
controlName.dataBind()
You can assign the following to the dataSource property:
- Collections
- Arrays
- DataSets
- DataReaders
- DataViews
- Enumerations
The control retrieves the data from the data source when you call the dataBind() method
You do not have to rebind the control and the data source every time the page is requested because the View State remembers the items and displays them

Templates and DataBinding
All the data bound controls support templates except for the treeView
The Repeater, DataList and FormView require templates
Templates are used to format the appearance and layout of each of the data items.
You can use data binding expression to display the value of the data within the templates
A template can contain HTML, DataBinding expressions and other controls, even data bound controls.

Data Binding Expressions
DataBinding expressions are not evaluated until runtime
Example:

Underneath the Eval() method calls the dataBinder.Eval() method
So:
<%# Eval("Survey_ID") %>
Is the same as:
<%#DataBinder.Eval(Container.DataItem,”Survey_id”)%>
ASP.Net 1.1 made you use DataBinder.Evel()
ASP.Net 2.0 provides a shorter method
You can also use formatting:
<%#Eval(“DataEntered”,”{0:D}”)%>
You can call other methods besides Eval()
For Example
Perhaps you have a function that changes a string to lowercase
Public Function Lowerit(by val myString as Object) as String
Return myString.toString().toLower()
End function
Then you can use this Binding Expression:
<%# Lowerit(Eval(“SurveyName”))%>

Two way DataBinding Expressions
In a one way dataBinding expression you can use dataBinding to display a value
In a two way dataBinding expression you can use dataBinding to display a value AND you can modify the value of the data item
