ASP.NET Dataset row count

The DataSet in ADO.NET can be filled with data in two ways: from a data source or dynamically. It serves as an in-memory representation of data that includes rows, columns, primary keys, constraints, and relationships with other DataTable objects.

Rows.Count property

To determine the number of rows in a specific table within a DataSet, you can utilize the Rows.Count property of the respective DataTable. Each DataTable within the DataSet represents a table, and the Rows property of the DataTable provides access to the collection of rows in that table. By retrieving the Count property of the Rows collection, you can obtain the total number of rows in the table.

VB.Net
ds.Tables(0).Rows.Count
C#
ds.Tables[0].Rows.Count

The following ASP.NET program shows how to find the number of rows in table inside a Dataset.

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; using System.Data ; using System.Data.SqlClient ; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString(); SqlConnection connection = new SqlConnection(connectionString); DataSet ds = new DataSet (); string sql = "select pub_name from publishers"; try { connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sql,connection ); adapter.Fill(ds); Label1.Text = "Number of rows " + ds.Tables[0].Rows.Count.ToString(); connection.Close(); } catch (Exception ex) { Label1.Text = "Error in execution " + ex.ToString(); } } }
Full Source | VB.NET
Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionString As String Dim connection As SqlConnection Dim ds As New DataSet Dim i As Integer connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString connection = New SqlConnection(connectionString) Dim sql As String = "select pub_name from publishers" Try connection.Open() Dim adapter As New SqlDataAdapter(sql, connection) adapter.Fill(ds) Label1.Text = "Number of rows " + ds.Tables(0).Rows.Count.ToString connection.Close() Catch ex As Exception Label1.Text = "Error in execution " & ex.ToString End Try End Sub End Class

Conclusion

The DataSet in ADO.NET can be filled with data from a data source or dynamically. It contains multiple DataTable objects, each representing a table. To get the number of rows in a specific table within the DataSet, you can access the Rows collection of the DataTable and retrieve the Count property. This provides the total count of rows in the table.