MDL
MDL

Reputation: 271

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

Upvotes: 0

Views: 77353

Answers (3)

JJC
JJC

Reputation: 65

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

The above code will work, however you need to give the table an identifier, such as:

tbRecordsFound.Text = ds.Tables("TableName").Rows.Count

This can be done via creating a DataAdapter and using the "Fill" function to give the table a name. Here is an example where "da" represents the DataAdapter:

da.Fill(ds, "TableName")

Upvotes: 1

royhette agapito
royhette agapito

Reputation: 21

For i As Integer = 0 To yourdatagridviewName.Rows.Count() - 1 Step +1
        i = +i
        TextBox2.Text = i 
    Next

Upvotes: 0

James Johnson
James Johnson

Reputation: 46057

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()

Upvotes: 4

Related Questions