How to get row count of ObjectDataSource
Asked Answered
E

7

10

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 .
Elielia answered 13/12, 2009 at 16:0 Comment(0)
F
8

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
Farleigh answered 23/6, 2011 at 19:24 Comment(0)
G
7

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.

Gelasias answered 2/1, 2010 at 23:42 Comment(0)
T
6

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;
}
Thessalonian answered 17/1, 2012 at 22:50 Comment(1)
You inspired a simpler solution for my application: private void MyObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e) { var count = e.ReturnValue as int?; this.litHitCount.Text = (!count.HasValue || count.Value == 0) ? "" : (count == 1) ? "1 hit" : $"{count} hits"; }Arsphenamine
E
0

You can achieve this very simply using a pager template e.g.

       <asp:DataPager PagedControlID="PagedControlId" PageSize="20" QueryStringField="QueryStringName" ID="InfoPager" runat="server">
           <Fields>
               <asp:TemplatePagerField>
                   <PagerTemplate>
                        Showing results 
                        <%=InfoPager.StartRowIndex + 1 %> 
                        to 
                        <%= (new []{(InfoPager.StartRowIndex + InfoPager.PageSize),InfoPager.TotalRowCount})
                                      .OrderBy(x => x)
                                      .First()%> 
                        of 
                        <%=InfoPager.TotalRowCount %>
                   </PagerTemplate>
               </asp:TemplatePagerField>
           </Fields>
       </asp:DataPager>

This will produce the text "Results x to y of z" including a check for the last page.

Cheers,

Ed

Epistyle answered 15/10, 2010 at 14:33 Comment(0)
R
0
Protected Sub objItemsList_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles objItemsList.Selected
    lblMessage.Text = DirectCast(e.ReturnValue, DataTable).Rows.Count & " record(s) found"
End Sub
Recoil answered 12/2, 2012 at 19:7 Comment(1)
Welcome to stackoverflow! It's always better to provide a short description for a sample code to improve the post accuracy :)Faa
F
0

In my case the ObjectDatasource is a dataset, so I get row number in objectdatasource_selected event like this

e.ReturnValue.tables(0).rows.count.ToString 
Fendig answered 11/3, 2014 at 8:37 Comment(0)
S
0
Protected Sub myODS_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles myODS.Selected
    Dim s As String = e.ReturnValue.ToString
    Dim rows As Integer
    Int32.TryParse(s, rows)

    'rows variable now holds the total number of results, not just what's displayed on the current gridview page
End Sub
St answered 10/5, 2016 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.