Home » CodeDigest


Get Number of Rows/Records the DataSource control returns in ASP.Net
Submitted By Satheesh Babu B
On 11/15/2008 6:48:38 AM
Tags: CodeDigest,DataSourceControl  

Get Number of Rows/Records the DataSource control returns


DataSource controls are new set of data controls released with .Netframework 2.0. These datasource controls greatly simplify the mostly used database interactions. Its just like an another ASP.Net control where we will configure the control to connect to the database and fetch whatever information we need without writting any code.

Sometimes, when we use sqldatasource control to bind the database data in our applications, we will require the number of records that datasource controls returns for the query we have specified.
The below code snippet will help us to fetch the number of rows the query returns using SqlDataSource control.


 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT * FROM [Employees]"  onselected="SqlDataSource1_Selected"></asp:SqlDataSource>

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        lblNoofPicz.Text = e.AffectedRows.ToString();
e.AffectedRows property in DataSource selected event will actually return the number of rows.
Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..