// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
Google / Android
Python / IronPython
SQL Server
Using SqlDataSource Declaratively
This quick snippet shows how to get data using SqlDataSource and attach it to a Repeater to display that data.
Table of Contents

Property: ConnectionString

Property: SelectCommand

Property: DataSourceMode


The SqlDataSource control is listed under Data category in the Toolbox. First step is to drag this control on to the Design surface of form (it can also be dragged into the Source of a Form). There will be no visual rendering of SqlDataSource control in the Design pane of the form. However, you can see this control listed in the Properties box for that form.

For example, you will see something like the following:

SqlDataSource1 System.Web.UI.WebControls.SqlDataSource

If you choose the above control in the Property box, you can see that control shown at the bottom strip bar of the Design window. From the Source pane, however, you can see the complete declaration of the SqlDataSource control, like the following:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

To make the above control get data from the database, you need to set the following properties:

Property: ConnectionString

This is how the database is accessed. You can type in the full string or select the connection string from your config file (shown in the dropdown for this property). An example string:

ConnectionString="Data Source=mydb;Initial Catalog=mycat;Persist Security Info=True;User ID=uid;Password=pword"

If you chose a connection string from the config file, it will be something like:

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

Property: SelectCommand

In this case we are getting data from the database. So we need to set up a SELECT query.

For example: SelectCommand="SELECT Name, Address FROM Persons"

Property: DataSourceMode

Two modes are available to set the behavior this data source. Do you want just a read-only listing of the records it obtained from the database OR Do you want the data to be editable as well? For read-only, choose DataReader; and for read-write, choose DataSet. Since we just want to display data in this case, choose DataReader.

For example: DataSourceMode="DataReader"

That is it! Your SqlDataSource will get the data from the database when you want it. Your SqlDataSource declaration will look something like this:

<asp:SqlDataSource ID="sdsCustomer" runat="server"
ConnectionString="Data Source=mydb;Initial Catalog=mycat;Persist Security Info=True;User ID=uid;Password=pword"
DataSourceMode="DataReader" ProviderName="System.Data.SqlClient"
SelectCommand="SELECT Name, Address FROM Persons">

Now attaching it to Repeater is as simple as setting the DataSourceID:

<asp:Repeater ID="rpt1" runat="server"
DataMember="DefaultView" DataSourceID="sdsCustomer" OnItemCommand="...">

Bookmark and Share This

More Articles With Similar Tags
You have defined a stored procedure that takes parameters. You have created an SqlDataSource that uses these parameters. Now how do you change these parameters at run-time from C# (or VB.Net) code?
Storing HTML in the database with all the HTML tags is straight-forward; however if the entered text has some tags (either HTML or XML) that are to be displayed as they are (i.e. not interpreted as formatting tags), you will run into some trouble with the browser. This article discusses a couple of options in dealing with these issues.
About  Contact  Privacy Policy  Site Map