Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I count the number of rows in a table? 1

Status
Not open for further replies.

OhioSteve

MIS
Joined
Mar 12, 2002
Messages
1,352
Location
US
How do I count the number of rows in a table?

The table is in a dataset.
I fill the table just before counting the rows.
I know (because this is test data) that the table should contain one row.
I keep getting 0 as the value.
Here are my failed attempts to count the table's rows:

myCount = ds.Tables.Item(0).Rows.Count
myCount = ds.Tables("salesAgents").Rows.Count
 
First check your query to make sure you are getting data. Run it in QA if you are using sql server.

Second, post all of your code.

Jim
 
If you have a look at the Tables property, you will see that it expects an Integer value e.g.
Code:
Dim i As Integer = ds.Tables(0).Rows.Count


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
My problem must be outside of that one line. Luckily, this is a pretty short function. So I can show you the whole thing. I have verified that "windowsID" and "serverConnection" are the appropriate values. You can examine the rest for yourself...

Dim da As New System.Data.SqlClient.SqlDataAdapter
Dim selectCommand As New System.Data.SqlClient.SqlCommand
da.SelectCommand = selectCommand
Dim pm As New SqlClient.SqlParameter
With pm
.ParameterName = "@agentName"
.SqlDbType = SqlDbType.VarChar
.Value = windowsID
End With
With selectCommand
.Connection = serverConnection
.Parameters.Add(pm)
.CommandText = "SELECT agentName, managerName FROM salesAgents WHERE (agentName = @agentName)"
.Connection = serverConnection
End With
Dim localDS As New FIRS_dev20.DataSet1
With da
.FillSchema(localDS, SchemaType.Source, "salesAgents")
.Fill(localDS, 0, 999, "salesAgents")
End With
Dim myCount As Int16
'myCount = ds.Tables.Item(0).Rows.Count
'myCount = ds.Tables("salesAgents").Rows.Count
' myCount = ds.Tables(0).Rows.Count
myCount = ds.Tables(1).Rows.Count
 
You are counting the rows from a table in a DataSet named "ds" but I can't see where that ds is created or populated...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
You are right that was part of the problem. I eliminated my sql 'where' clause and changed the count mechanism as follows~
myCount = localDS.Tables("salesAgents").Rows.Count

I got TWO as the number, which is correct (if you do not have a where clause). I am working on the where clause now.
 
Okay I fixed the other line! It should be~

.CommandText = "SELECT agentName, managerName FROM salesAgents where (agentName = @agentName)"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top