Reputation: 267
So im pulling data from a db and then counting all the data on that db but it counts before the data is pulled. Anyway to stop this?
test.SelectCommand = Ssql
test.SelectParameters.Clear()
test.DataBind()
System.Threading.Thread.Sleep(1000)
counter = OSG.Items.Count
MsgBox(counter)
cheers!
Upvotes: 1
Views: 118
Reputation: 1732
Count the data source not the items in the datagrid. The datagrid binding will happen asynchronously and will not be accurate. For example if your data source is a datatable then you can:
Dim x As DataTable = New DataTable
Dim counter As Integer
counter = x.Rows.Count
EDIT: To count rows in a SQL data source you have to handle the 'selected' event. Make a class level variable called myRowCount, set it in the selected event and use it after the bind statement:
Dim myRowCount As Integer
Protected SubSqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
MsgBox(e.AffectedRows())
myRowCount = e.AffectedRows()
End Sub
Then you can use myRowCount elsewhere in the code.
Upvotes: 3