Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

Database Programming - Core Data Controls

The document discusses database programming in ASP.NET, including different types of data source and data bound controls. It describes how controls like SqlDataSource facilitate database connections and how databound controls generate user interfaces for working with data. Specific controls are explained, including how to display, sort, page and select data with the GridView control. The document also covers storing database connections in the web.config file for security and manageability.

Uploaded by

Shivani Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Database Programming - Core Data Controls

The document discusses database programming in ASP.NET, including different types of data source and data bound controls. It describes how controls like SqlDataSource facilitate database connections and how databound controls generate user interfaces for working with data. Specific controls are explained, including how to display, sort, page and select data with the GridView control. The document also covers storing database connections in the web.config file for security and manageability.

Uploaded by

Shivani Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Database Programming

Core Data Controls


Types Data control in Visual Studio
DataSource Controls
 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.
Databound Control
 DataBound controls helps to generate application’s
user interface for working with data. The
DataBound controls can be used to display and edit
database data / XML data.
DataSource Controls
Facilitates connections

Control set of commands executed over


DB

Helps to connect SQL and non SQL DBs


Different DataSource Controls
 AccessDataSource SqlDataSource control when working with
Microsoft Access, the ASP.NET Framework does include the
AccessDataSource control, which was designed specifically for
Microsoft Access.
 
 SqlDataSource control -The SqlDataSource control is built on top
of ADO.NET. Under the covers, the SqlDataSource uses ADO.NET
objects such as the DataSet, DataReader, and Command objects.
Because the SqlDataSource control is a control, it enables you to
use these ADO.NET objects declaratively rather than
programmatically.

 ObjectDataSource- The SqlDataSource control is not an


appropriate control to use when building more complicated multi-
tier applications. If you want to build a more cleanly architected
multi-tier application, then you should use the ObjectDataSource
control to represent your database data.
Working with SqlDataSource
Creating Database Connections

Local DB
ConnectionString=
“Data Source=.\SQLEXPRESS; AttachDbFilename=|
DataDirectory|MyDatabase.mdf; Integrated
Security=True;
User Instance=True”
Working with SqlDataSource
Creating Database Connections

Server DB
ConnectionString=“
Data Source=ServerName;
Initial Catalog= DatabaseName;
User ID=SQLServerUserName;
Password=Password”
Working with SqlDataSource
Connecting to Other Databases-Oracle
The .NET Framework includes the following providers:
System.Data.OracleClient—Use the ADO.NET
provider for Oracle when connecting to an Oracle
database.
System.Data.OleDb—Use the OLE DB provider when
connecting to a data source that supports an OLE DB
provider.
System.Data.Odbc—Use the ODBC provider when
connecting to a data source with an ODBC driver.
Working with SqlDataSource
Connecting Oracle Database

<asp:SqlDataSource id=“srcOrders”
ProviderName=“System.Data.OracleClient”
SelectCommand=“SELECT * FROM Orders”
ConnectionString=“Data Source=OracleDB;
Integrated Security=yes” Runat=“server” />
There are three main types of DataBound
controls:
 
List DataBound Controls

Tabular DataBound Controls

Hierarchical DataBound Controls


List DataBound Controls

List controls are used to display simple option lists -


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.
Tabular DataBound Controls
The tabular DataBound controls are the
main set of controls that we use when
working with database data. These
controls enable us to display and, in some
cases, modify data retrieved from a
database or other type of data source.
Tabular DataBound Controls
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.
Tabular DataBound Controls
ListView—Displays a set of data items using a
template. Unlike the Repeater control, the ListView
control supports sorting, paging, and editing
database data.
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.
GridView Control
The GridView control is the most
powerful control / workhorse of the
ASP.NET Framework. It is one of the
most feature-rich and complicated of all
the ASP.NET controls. The GridView
control enables us to display, select, sort,
page, and edit data items such as
database records.
GridView Control
-Displaying Data
The GridView renders its data items
in an HTML table. Each data item is
rendered in a distinct HTML table
row. The GridView is associated with
its data source through its
DataSourceID property
GridView Control
-Displaying Empty Data
The GridView includes two properties that
enable us to display content when no results
are returned from the GridView control’s data
source. We can use either the EmptyDataText
property or the EmptyDataTemplate property
to handle empty data.

EmptyDataText=”<img src=’sad.gif’/> No
Matching Movies!”
GridView Control
-Selecting Data
enable a user to select a particular row in
a GridView control. This is useful when
you want to build single-page
Master/Details forms.
first GridView has its
AutoGenerateSelectButton property
enabled. When this property has the value
True, a Select link is displayed next to
each row.
GridView Control
-Selecting Data
We can determine which row is selected in
a GridView control by using any of the
following methods:
SelectedDataKey()—Returns the DataKey
object associated with the selected row (useful
when there are multiple data keys).
SelectedIndex()—Returns the (zero-based)
index of the selected row.
SelectedValue()—Returns the data key
associated with the selected row.
GridView Control
-Selecting Data
Data Keys
We associate a value with each row in a
GridView by providing a value for the
GridView control’s DataKeyNames
property. We can assign the name of a
single database column to this property or
we can assign a comma-separated list of
column names to this property.
GridView Control
-Sorting Data
We can sort the rows rendered by a
GridView control by enabling the
AllowSorting property.
GridView Control
-Paging Through Data
When working with a large number of
database rows, it is useful to be able to
display the rows in different pages. We
can enable paging with the GridView
control by enabling its AllowPaging
property.

AllowPaging=”true” PageSize=”5”
GridView Control
- Customizing Paging
 Customizing the Paging Interface By default, when
paging is enabled, the GridView renders a list of page
numbers at the bottom of the grid. We can modify the
user interface for paging through records by modifying
the GridView control’s PagerSettings property.

 PageSize=”3”
 PagerSettings-Mode=”NextPreviousFirstLast”
 PagerSettings-Position=”TopAndBottom”
 PagerStyle-HorizontalAlign=”Center”
GridView Control
- PagerSettings class
The PagerSettings class supports the following properties:
 
 FirstPageImageUrl—Enables you to display an image for the first page link.
 FirstPageText—Enables you to specify the text for the first page link.
 LastPageImageUrl—Enables you to display an image for the last page link.
 LastPageText—Enables you to specify the text for the last page link.
 Mode—Enables you to select a display mode for the pager user interface. Possible
values are NextPrevious, NextPreviousFirstLast, Numeric, and NumericFirstLast.
 NextPageImageUrl—Enables you to display an image for the next page link.
 NextPageText—Enables you to specify the text for the next page link.
 PageButtonCount—Enables you to specify the number of page number links to
display.
 Position—Enables you to specify the position of the paging user interface. Possible
values are Bottom, Top, and TopAndBottom.
 PreviousPageImageUrl—Enables you to display an image for the previous page
link.
 PreviousPageText—Enables you to specify the text for the previous page link.
 Visible—Enables you to hide the paging user interface.
Storing Connection in Web.config
Storing connection string in webpages
unsafe.
◦ Security
◦ Performance
◦ Difficult to manage
So better to store connection in configuration
file of Project
Storing Connection in Web.config
Web.Config;
 
<configuration>
<connectionStrings>
<add name=”Movies” connectionString=”Data
Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|
MyDatabase.mdf;Integrated Security=True;
User Instance=True” />
</connectionStrings>
</configuration>

You might also like