You are on page 1of 20

ADO.NET in ASP.

NET
• As we have seen before, we can program using
ADO.NET in two ways
• By explicitly writing code to create
Connection, DataReader,
DataAdpater etc. and then binding them
with controls  traditional way
• By dragging and dropping controls and
configuring them visually.  DataBinding 
New feature in ASP 2.0.
• We will use the 2nd approach in this session.
DataBinding
• ASP.NET includes a rich and full-featured model
for data binding.
• Data binding is a technique of binding asp.net
web controls to the Data Source controls so that
data is automatically fetched and shown.
• Data Source controls are the controls that links
itself with data sources like SQLserver, Oracle,
Access etc.
• Data binding is declarative, not programmatic.
Data Source Controls
Special data controls

DataSource controls

• Most of standard web controls (like TextBox, Label, LinkButton


etc., )support single-value data binding.
• Controls like ListBox, DropDownList support multi-value.
• As with windows form application, for web application also
there is a GridView which is specifically data-bound control.
• But apart from GridView, the diagram shows other controls
also which are available for the web form.
Creating SQLServer Database
tables
• Let us create two tables in a SQLServer database.

• Enter some sample data.


Creating DataSource control
• Create a new asp.net web project.
• Drag and drop SQLDataSource Control in
Default.aspx.
• Dragging a dropping the control leads to entry of a new
line in the source
• <asp:SqlDataSource ID="SqlDataSource1"
runat="server"></asp:SqlDataSource>
Configuring the DataSource
1. Right click and click on Configure Data Source.
2. Select the database that you configured.

• Ensure that this is


ticked. This makes an
entry in web.config file.
1. Select table and the columns.
2. Click on Advanced and tick this

1. Test Query and click on Finish


Connection string entry in web.config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="EmployeeConnectionString"
connectionString="Data Source=DEEPA-
PC\VAIO_VEDB;Initial
Catalog=Employee;Integrated Security=True"

providerName="System.Data.SqlClient" />
</connectionStrings>

• This is what gets automatically added in web,config file
because of step 3.
What is the advantage of this?
Viewing the properties of
DataSource control

Similarly look at the InsertQuery and Update Query


too. This happened because of step 5
Viewing data source code in aspx
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:EmployeeConnectionStri
ng %>" a $ expression is a code sequence that
you can add to an .aspx page which will
be evaluated by an expression builder
when the page is rendered
DeleteCommand="DELETE FROM [Employee]
WHERE [EID] = @EID"

InsertCommand="INSERT INTO [Employee]


([EID], [Name]) VALUES (@EID, @Name)“
Parameters are indicated with an @. In the previous diagram we have seen that
Parameter Source is ‘None’.
If parameter name is the same name as the field, you don’t need to define the
parameter. That’s because the ASP.NET data controls
automatically submit a collection of parameters with the new values before
triggering the commands. Each parameter in the collection uses this naming
convention.
SelectCommand="SELECT [EID], [Name] FROM
[Employee]"
UpdateCommand="UPDATE [Employee] SET [Name] = @Name
WHERE [EID] = @EID">

<DeleteParameters>
<asp:Parameter Name="EID" Type="Decimal" />
</DeleteParameters>

<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="EID" Type="Decimal" />
</UpdateParameters>

<InsertParameters>
<asp:Parameter Name="EID" Type="Decimal" />
<asp:Parameter Name="Name" Type="String" />
</InsertParameters>
Data types enforced
</asp:SqlDataSource>
for the parameters
Binding GridView
• Drag and drop GridView
from the Data Controls

• Select the DataSource and


Enable all the checkboxes.

• Select the look and


feel
Build and Execute

Edit and Upadate

Note that the GridView was not designed


to insert new rows.

After delete
Specifying form parameters in the
select query
• Drag and drop a text box and a button on to the form.
• Select property of the SQLDataSource and go to select
query.

• Click on Query builder.


Building Query
• Enter the where clause and
click ok.
• Click on refresh
parameters.
• Select Control for
Parameter Source and
select the name of textbox
control you dropped for
controlID.
• Click OK
Execute
Types of parameters

• Apart from Control, parameter for the query could be


HTML Form parameters, Query String, Session data,
Cookie or a key from user profile.
• Code that just got added
<SelectParameters>
<asp:ControlParameter
ControlID="TextBox1" DefaultValue="0"
Name="ID" PropertyName="Text" />
</SelectParameters>
Form Control
• Create another Der form.
• Drag and drop the SQLDataSource and configure in the
same way.
• Drag and drop FormView and link it to the data source.
• Apply template , enable paging.
• Test

Try inserting,
updating and
deleting
Using stored procedures instead of
sql commands
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:EmployeeConnectionString
%>" …
UpdateCommand="UpdateEmployee"
UpdateCommandType="StoredProcedure“>

</asp:SqlDataSource>

Stored procedure name


protected void UpdatEEMP(object sender,
SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["@Name"].Value =
e.Command.Parameters["@FirstName"].Value;
e.Command.Parameters["@Last"].Value =
e.Command.Parameters["@LastName"].Value;
e.Command.Parameters.Remove(e.Command.Parame
ters["@FirstName"]);
e.Command.Parameters.Remove(e.Command.Parame
ters["@LastName"]);
}

You might also like