Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get row count of ObjectDataSource

Hello you all

How can i get row count of ObjectDataSouce ?

I use ObjectDataSource and DataList . I want show some thing to the user for example in a label when there are certain row returned by ObjectDataSource . One of situation is when there is no record .

Thank you .
like image 739
Mostafa Avatar asked Dec 13 '09 16:12

Mostafa


3 Answers

Found this here:

bool bGetSelectCount;
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    if (bGetSelectCount)
        TextBox1.Text = e.ReturnValue.ToString(); 
}

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    bGetSelectCount = e.ExecutingSelectCount;
}
like image 120
Andrew Huey Avatar answered Oct 23 '22 14:10

Andrew Huey


I was looking for the same answer... Another solution I ended up using is the following: This is found on a .vb file behind an .aspx page. It handles the "selected" event of the datasource.

Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSource1.Selected
    ' Select data for rowcount
    Dim dt As DataTable = e.ReturnValue
    ' Set row count label
    Me.lblCount.Text = dt.Rows.Count.ToString
End Sub
like image 35
Watki02 Avatar answered Oct 23 '22 14:10

Watki02


The ObjectDataSource does not have a direct way to get the total row count. One of the reasons for this is that if all you want is the total row count then you don't need the data source at all! To get the row count just talk to your Business Logic Layer (BLL) and get the total rows:

MyBLL bll = new MyBLL();
int customerRowCount = bll.Customers.GetRowCount();

The ObjectDataSource does have a SelectCountMethod that can be used when data bound controls such as the GridView need to access the total row count. However, this is only used while also performing a Select operation. That is, there is no way to only get the row count. The row count is only used so that the data bound control can display a pager control - it is not used for anything else.

like image 7
Eilon Avatar answered Oct 23 '22 12:10

Eilon