Thursday, April 3, 2008


The DataSource control (new in ASP.net) is a real timesaver for programmers.
Now, instead of writing all the code to populate a grid, it's possible to use
a DataSource control, a new 'genre' (if you will) of controls, which
provide 'declarative' access to data. Though I won't go into all of them
(there are several - SQL, Access, XML, SiteMapDataSource and ObjectDataSource
controls), I will cover the Database type DataControl (SQLDataSource
and AccessDataSource). The properties are similar for both.

In general, at the basic level, what happens is that each DataSource
control has many properties which the programmer populates, like the
ConnectionString property and the SQL statement properties (SelectCommand,
UpdateCommand, etc).


Once these properties have been provided, a presentation control (like a
GridView, FormView or DetailsView control, all new in v2.0 of the Dotnet
Framework) can point to the DataSource control, and without any coding at all
- voila - a web page has been created that displays data from a data source.
Naturally, it doesn't do much except presentation, at this point, but like was
said earlier, this is just the basic leve..


For the SelectCommand, one can choose a pure SQL Select statement (Select
[field name list] from [Table name]), or a Stored Procedure. If a Stored
Procedure is used, the 'SelectCommandType' property needs to be set thusly:

 



SelectCommandType="StoredProcedure"


Here, we might add that a new section has been added to the Web.Config
file, specifically for Connection Strings. And, as you might figure, the
section is called 'ConnectionStrings', and is placed inside the Configuration
section of the Web.Config file, much like the AppSettings section was used
earlier, for the connection strings. The main difference here, is, instead of
'add key', we use 'add name':

 



<connectionStrings>
<add name="Pubs" connectionString="Server=YourServerIP;uid=YourUID;pwd=YourPWD;database=YourDB"
providerName="System.Data.SqlClient" />
</connectionStrings>


As you can also see (above), the providerName attribute is added. With
this, the page never needs to bother Importing the Namespaces, which are
specific to the particular data connection. Of course, for Access, you'd use
OleDb, here, instead of SQLclient.


To address that ConnectionString, in your DataSource control, (notice the
new tag definition) merely do something like this:



ConnectionString="<%$ ConnectionStrings:Pubs %>"


Of course, if you would rather do it manually, you can just code the
connection string manually here, also.


Now - you are probably saying to yourself, "That's easy enough, but what
about Where clauses?". That's fairly easy also, at least easier than it was in
the past. The DataSource controls have other attributes, especially for this.
For the SelectCommand, there is a SelectParameters attribute (for
UpdateCommand, UpateParameters, for the DeleteCommand, DeleteParameters, etc).
Here's an example:



<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:Pubs %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE [state] = @state">
<
SelectParameters>
<
asp:ControlParameter Name="state" ControlID="ddlStates" PropertyName="SelectedValue" />
</
SelectParameters>
</
asp:SqlDataSource>


Here, we set a SelectParameter, with the ControlID, point it to the control
which the Where Clause will get it's defining data, and assign the 'SelectedValue'
(or 'SelectedItem.Text'), to the Property name attribute of the
SelectParamenter. That's really all there is to it!


At this point, all that's left to do is to assign this DataSource ID to the
DataSource property of the presentation control of your choice, like so:



<asp:GridView id="MyGridView" RunAt="Server" DataSourceID="SQLDataSource1"/>


Also - one property of the DataSource Control is DataSourceMode. Here, you
can assign 'DataReader' or 'DataSet', though, from what I can tell, the
default is 'DataSet', so you really don't need to assign the property at all,
if that's what you want. And, if you want paging and sorting, that IS
what you will want.


Technically, no coding was placed on the page, even though,
with just 2 controls, a DataSource Control, and a GridView, it's easy to
display the received data from your database!



As an additional note here, there are a number of other Parameter Classes
which are able to be used here, like SessionParameters,
QueryStringParameters, CookieParameters and FormParameters. Here, we only
looked at the SelectParameters attribute, based on using the Selectcommand,
due to the sample shown. However, there are also the following Commands,
used in the DataSource Controls:

InsertCommand, UpdateCommand and Deletecommand

Each of those can use the different Parameter Classes mentioned here,
depending on the use and needs for the individual DataSource needs.