0% found this document useful (0 votes)
10 views

DataList, Data binding, DataSource

The document explains how to use custom SQL statements and stored procedures in ASP.NET to interact with SQL data sources, including the process of entering custom statements and utilizing the DataList control for displaying data. It also covers data binding techniques, including one-way and two-way binding, and advanced features of SQL data sources such as updating databases and caching data for performance improvement. Overall, it provides a comprehensive guide for managing data interactions in web applications.

Uploaded by

Midhun Manoj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DataList, Data binding, DataSource

The document explains how to use custom SQL statements and stored procedures in ASP.NET to interact with SQL data sources, including the process of entering custom statements and utilizing the DataList control for displaying data. It also covers data binding techniques, including one-way and two-way binding, and advanced features of SQL data sources such as updating databases and caching data for performance improvement. Overall, it provides a comprehensive guide for managing data interactions in web applications.

Uploaded by

Midhun Manoj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

How to use custom statements and stored procedures

how to configure the Select statement for a SQL data source


by selecting columns from a table. If you need to code a
Select statement that's more complex than what you can
create using this technique, you can define your own castom
statements. To do that, you can enter the statements
directly, or you can use the Query Builder. You can also use
stored procedures that have been defined within the
database.
custom statements
These are SQL commands (like SELECT, INSERT, UPDATE,
DELETE) that you manually define instead of relying on
automatic ones created by ASP.NET tools.
How to enter custom statements
Using the Configure Data Source Wizard:
You open the wizard to set up a data source.
At one step, you choose to enter your SQL statements
manually.
A dialog box appears with options to enter custom
commands for selecting, updating, inserting, or deleting
data.
For example, you can write something like:
SELECT * FROM Products WHERE CategoryID =
@CategoryID
Here, @CategoryID is a parameter whose value comes from
user input or another control.
Why use custom statements?
The automatically generated SQL statements are basic and
may not meet all your needs.
If your database logic is complex (e.g., filtering, joining
multiple tables), custom statements let you define that logic
yourself.
How to use stored procedures instead of custom SQL?
What are stored procedures?
Predefined SQL code saved in the database, like reusable
scripts.
How to use them:
In the wizard, select “Stored Procedure” instead of writing
SQL directly.
Choose the stored procedure you want to use from the list
provided.
Parameters for the procedure are set automatically.
HOW TO USE DATALIST CONTROL
What is the DataList control?
The DataList control in ASP.NET is used to display a list of
data (like rows from a database) in a repeating format, such
as a table or custom layout.
It’s ideal for creating structured lists, where you can
customize how each item is displayed.
How the DataList control works:
1. Connect to a Data Source:
The DataList gets data from a database or another source
using a DataSourceID property.

2. Define the Layout:


Inside the DataList, you decide how each item (like a
product or row) will look using an ItemTemplate. For
example, you can display a name and price.

3. Fetch and Display:


When the page loads, the DataList automatically fetches the
data and shows it on the page, repeating the layout for each
item in the data.
Description
A data list displays a list of items from the data source that
it's bound to. To bind a data list to a data source, use the
Choose Data Source command in the control's smart tag
menu.
To define the information to be displayed in a data list, you
create one or more templates.
To display the data from a column in the data source in a
data list, you add a control to a template and then bind that
control.
You can use a DataList control for edit operations as well as
display operations. How- ever, you're more likely to use the
GridView, Details View, FormView, and ListView controls for
edit operations.
How to define template for Datalist
The DataList uses templates to decide how each row of data
will look.
The most commonly used template is the ItemTemplate,
which defines the layout for each item.
Description
The templates you define for a data list specify what content
to display and what controls to use to display it. At the least,
you must create an Item template that defines the items
from the data source that you want to display.
To create a template, choose Edit Templates from the smart
tag menu for the control to display the control in template-
editing mode. Then, select the template or group of
templates you want to edit from the smart tag menu.

HOW TO FORMAT A DATALIST


To format a DataList in ASP.NET, you can use HTML, CSS,
and built-in templates to control the layout and design.
Here’s how you can format a DataList to make it look better
and more organized:
1. Use CSS for Styling
You can use CSS to apply styles to the DataList and its
templates.
2. Format Item Templates
Inside the ItemTemplate, you can add HTML tags and CSS to
control the appearance of the data.
Description
You can format a data list by applying a predefined scheme,
by using the Properties.
Dialog box, by using the Properties window, or by editing the
aspx code.To apply a scheme, choose Auto Format from the
control’s smart tag menu and then select the scheme you
want to apply.
DATA BINDING
Data binding in web development refers to the process of
linking data (usually from a database or other data sources)
to user interface (UI) elements, such as controls and
components, on a webpage.
In simple terms, data binding automatically populates UI
elements (like text boxes, labels, lists, etc.) with data from a
data source, such as a database, a file, or an object.

How to use Data binding


1. One-way binding:
Data is pulled from the data source to the control.
Example: Displaying a product name in a label.
<asp:Label ID=”Label1” runat=”server” Text=’<%#
Eval("ProductName") %>' />
2. Two-way binding:
Data is pulled from the source to the control, and any
changes made by the user in the control are automatically
saved back to the data source.
Example: Editing a product name in a textbox.
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("ProductName") %>' />
3. Grid or List:
For displaying lists or tables (like a list of products), you use
controls like GridView or Repeater.
Example: Binding data to a GridView.
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" />
How to Use It:
1. Set the DataSource (e.g., a database or object).
2. Use Eval() for one-way binding (display data).
3. Use Bind() for two-way binding (display and update data).
4. Call DataBind() to refresh the control with the data.
Description
You can bind any of the controls that inherit the ListControl
class to a data source. That includes the list box control, the
drop-down list control, the check box list control, the radio
button list control, and the bulleted list control.
How to bind the controls in a template
To bind controls in a template (such as in a GridView, or
DataList), you define the layout of the control inside the
template and then bind each control to a data field from your
data source.
Description
• To bind a control in a template, select the Edit
DataBindings command from the smart
• tag menu for the control to display the DataBindings
dialog box. Then, select the prop- erty you want to bind
to (usually Text), select the Field Binding option, and
select the field you want to bind to from the Bound To
drop-down list
• If you want to apply a format to the bound data, select a
format from the Format drop- down list
How to use the advanced features of a SQL data
source
The SqlDataSource control provides several advanced
features that you may want to use in your applications.
These features are explained in the topics that follow.

How to create a data source that can update the


database
SQL data source can include Insert, Update, and
Delete statements that let you automatically update the
underlying database based on changes made by the
user to bound data controls.
Note that for this to work, the primary key column of the
table you’re updating must be included in the Select
statement. That’s because this column is used to
identify a row that’s being updated or deleted. So if the
Generate option in the Advanced SQL Generation
Options dialog box isn’t enabled, it’s probably because
you haven’t selected the primary key column.
Description
To automatically generate Insert, Update, and Delete
statements for a data source, check the first box in the
dialog box that you get by clicking on the Advanced
button in the first. dialog box in figure. . To generate
enhanced versions of the Update and Delete state-
ments that use optimistic concurrency, check the
second box too.
How to change the data source mode
When you create a SQL data source, the data is
retrieved into a dataset by default. If the data will be
read just once and not updated, though, you can
usually improve the application’s performance by
retrieving the data using a data reader.

Description

The DataSourceMode attribute lets you specify that


data should be retrieved using a data Reader rather
than being stored in a dataset. For read-only data, a
data reader is usually more efficient.
How to use caching
ASP.NET’s caching feature lets you save the data
retrieved by a data source in cache memory on the
server. That way, the next time the data needs to be
retrieved, the cached data is used instead of getting it
from the database again. Since this reduces database
access, it often improves an application’s overall
performance.

Description
The data source caching attributes let you specify that data
should be stored in cache Storage for a specified period of
time. For data that changes infrequently, caching can
improve performance.

You might also like