Bind Data From a SqlDataSource to a Label
To programmatically access the contents of a Sql or Access DataSource control you need to explicitly call its Select() method. This method accepts a single input parameter of type DataSourceSelectArguments. This parameter can contain information regarding the filters to apply or the column to Order By. For example, when working with a sortable GridView, sorting a column calls the Select() method, and passes in a DataSourceSelectArguments instance with its SortExpression property set to the column name the user chose to sort by. If you don't want the DataSource to sort or filter, you pass in DataSourceSelectArguments.Empty.
Depending on the DataSourceMode of the DataSource control, one of two objects are returned when the Select() method is called. If the DataSourceMode is set to DataReader, a DataReader object is returned. The type of DataReader (SqlDataReader, OleDbDataReader, OdbcDataReader etc) that is returned depends entirely on the provider type used - in other words, whether you are using the OleDb provider, SqlClient provider etc. It has nothing to do with the type of DataSource control. The examples below both query an Access database, but one uses the AccessDataSource control, and the other uses the SqlDataSource control. Both return OleDbDataReaders, because it is the OleDbProvider library that is used for the connection.
If the DataSourceMode is set to Dataset, or not set at all (which means that the default setting of Dataset is used) the object that is returned is a DataView. A DataView is like a DataTable on steroids. It exposes methods that allow you to filter and sort data, for example, and bind it. A DataView contains a collection of DataRowView objects, which represent each row in the returned results.
So, with a DataReader, you would access the values during the DataReader.Read() operation, in very much the same way as if you are using plain ADO.NET code, whereas with the DataSet, you would need to create an object of the appropriate type - DataView, then iterate the DataRowView collection to access the values. In this, the code is remarkably similar to accessing values directly from a DataSet's table collection using plain ADO.NET.
The code below shows the contents of an aspx file, which contains two label controls, and two SqlDataSource controls. Each SqlDataSource control has its DataSource mode set to alternative values - DataReader and DataSet, and both of them have an OnSelecting event defined in which the value of the EmployeeID parameter is assigned:
<asp:Label ID="Label1" runat="server" /> <asp:Label ID="Label2" runat="server" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" DatasourceMode="DataSet" SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)" OnSelecting="SqlDataSource1_Selecting"> <SelectParameters> <asp:Parameter Name="EmployeeID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" DatasourceMode="DataReader" SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)" OnSelecting="SqlDataSource2_Selecting"> <SelectParameters> <asp:Parameter Name="EmployeeID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>
The following code snippet shows the aspx.cs file contents, where the parameter values are set in the Selecting event handler. In the Page_Load method, the data returned by each of the Sql DataSource controls is accessed and a value consigned to a label. The method of access depends on the DataSource mode, but is identical for both SqlDataSource and AccessDataSource:
[C#]
protected void Page_Load(object sender, EventArgs e)
{
DataView dvSql = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drvSql in dvSql)
{
Label1.Text = drvSql["FirstName"].ToString();
}
OleDbDataReader rdrSql =
(OleDbDataReader)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
while (rdrSql.Read())
{
Label2.Text = rdrSql["LastName"].ToString();
}
rdrSql.Close();
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["EmployeeID"].Value = 2;
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["EmployeeID"].Value = 2;
}
[VB]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim dvSql As DataView =
DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
For Each drvSql As DataRowView In dvSql
Label1.Text = drvSql("FirstName").ToString()
Next
Dim rdrSql As OleDbDataReader =
DirectCast(SqlDataSource2.Select(DataSourceSelectArguments.Empty), OleDbDataReader)
While rdrSql.Read()
Label2.Text = rdrSql("LastName").ToString()
End While
rdrSql.Close()
End Sub
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object,
ByVal e As SqlDataSourceSelectingEventArgs)
e.Command.Parameters("EmployeeID").Value = 2
End Sub
Protected Sub SqlDataSource2_Selecting(ByVal sender As Object,
ByVal e As SqlDataSourceSelectingEventArgs)
e.Command.Parameters("EmployeeID").Value = 2
End Sub
Currently rated 5.00 by 4 people
Rate Now!
Date Posted:
25 August 2007 10:32
Last Updated:
01 February 2009 20:01
Posted by:
Mikesdotnetting
Total Views to date:
25112
Printer Friendly Version
Comments
20 January 2009 04:07 from bradzmac
Let me try the code, I am intermediate programmer. hope this will help me. retirve a value to a database. a random value. i will modify the SQL syntax to fit my needs. thank you for the code.
23 January 2009 09:02 from Jake
Thank you so so much :-)
29 January 2009 18:55 from sameer
Thank you so much, I was almost dissapointed looking in google for the solution, everyone was just giving some answers for sake of answers, escpecially asp.net forums,
This is perfect answer, thanks again
27 February 2009 21:16 from Jayquest
Wow! How freaking easy is that! Why is it that everyone else on the internet insists that you can't do this without using a formview or dataview. I was getting pissed. Thanks a billion for explaining the process so well. I will visit your site more often now that I found it.
JayQuest
27 February 2009 21:16 from Jayquest
Wow! How freaking easy is that! Why is it that everyone else on the internet insists that you can't do this without using a formview or dataview. I was getting pissed. Thanks a billion for explaining the process so well. I will visit your site more often now that I found it.
JayQuest
19 August 2009 23:56 from Doug Moore
Hey Mike, this is such a great article, but I have two questions. I've tried your code example in my project, and it works great, but would like to know why. - - 1)Why do we need a foreach here: foreach (DataRowView drvSql in dvSql) when there is only one row that is returned? - - - - 2)Why do we need ToString() here: Label1.Text = drvSql("FirstName").ToString() Isn't "FirstName" already a string?
20 August 2009 20:36 from Mikesdotnetting
@Doug
1. That's a fair point. You could reference it like this instead:
Label3.Text = dvSql[0][1].ToString();
or
Label3.Text = dvSql[0]["FirstName"].ToString();
2. No - "FirstName" isn't a string. It's an index. Just as 0 is in the dvSql[0][0] option. The actual datatype that's returned is an object. If you try leaving .ToString() off, the compiler will complain.
14 February 2010 15:03 from Janosch
Awesome Mate! I need exactly this


09 January 2009 20:52 from Khaled
NEAT !! that's exactly what I was looking for.. a quick crash course !