Skip Navigation LinksASP.Net 2.0 Training : ASP.Net Data Access
Banner for ASP.Net 2.0 training classes in Visual Basic 2005 

Skip Navigation Links
Visit my other site:

certification training

Free Flash tutorials and training

Free Dreamweaver Video Tutorials

Free PHP Training

Visual Basic Windows Training




If you like this site please link to it


Data Access

There are 3 main types of dataBound controls

  1. List Controls
  2. Tabular databound controls
  3. Hierarchical databound controls

List Controls:

  1. BulletedList
  2. CheckboxList
  3. DropDownList
  4. Listbox
  5. RadioButtonList

Tabular DataBound Controls

  1. Display a set of data
    1. GridView
    2. DataList
    3. Repeater
  2. Display a single data item at a time
    1. DetailsView
    2. FormView

The DataGrid is included in ASP.Net 2.0 for backward compatibility. It is not recommended to use it

Hierarchical DataBound Controls

  1. Menu
  2. 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

  1. SQLDataSource
    1. Retrieve data from a SQL relational database
      • SQL Server
      • Oracle
      • DB2
  1. AccessDataSOurce
    1. Retrieve from a Microsoft Access database
  2. ObjectDataSource
    1. Retrieve data from a business object
  3. XMLDataSource
    1. Retrieve data from an XML document
  4. SiteMapDataSOurce
    1. 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
    • Static value
  • 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 Next

  • 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

  • Click OK

You where clause is now complete

  • Click Next

  • Click Finish

Here is the code:

  • Add a gridView control
  • Click Choose Data Source

  • Select the second dataSource you just created

  • Test it

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

Syntax:

<%#         %>

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


      ASP.Net 2.0 tutorials & training