MDL MDL - 11 months ago 52 Question

Count how many rows are in the dataset then display as text in a textbox?

How can I count how many rows are returned by a dataset and then show the total number of rows as textbox.text and read-only so that the user can only see them but not change them?

so far I have this but it dosent return a number and says it cant find table 0:

tbRecordsFound.Text = ds.Tables(0).Rows.Count

Answer Source

Try something like this:

tbRecordsFound.Text = ds.Tables.Cast<DataTable>().Sum(x => x.Rows.Count).ToString()

You can also do it like this:

Dim recordCount as Integer = 0;
For Each table as Datatable in ds.Tables
    recordCount += table.Rows.Count
tbRecordsFound.Text = recordCount.ToString()