[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Record Count

Posted on 2006-03-22
16
Medium Priority
?
514 Views
Last Modified: 2012-06-21
I am trying to fill a span with the record count of my grid view.  It kinda of works.  But here is the thing.  I have added paging displaying 10 records per page. The record count is for each page.  So it keeps being 10, until the last page where there are 3 records and the record count is three.

I need the record count to reflect the whole recordset not just the records per page.  The simple code I am using at the mo is this

        int RecCount = GridView1.Rows.Count;
        string WrRecCount = RecCount.ToString();
        totalRecords.InnerHtml = WrRecCount;
0
Comment
Question by:REA_ANDREW
  • 5
  • 4
  • 2
  • +3
16 Comments
 
LVL 18

Expert Comment

by:Elvio Lujan
ID: 16257756
if you are filling the gridview with a dataset you can do the count from the dataset or datatable!
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16257781
Do you know the code to do this?
0
 
LVL 18

Expert Comment

by:Elvio Lujan
ID: 16257796
sure... can post the portion of code where you fill the gridview?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16257805
<asp:GridView ID="GridView1"  DataSourceID="SqlDataSource1" runat=server />
<asp:SqlDataSource ID="SqlDataSource1" runat=server SelectCommand="SELECT [au_id],[au_lname],[au_fname],[phone],[address],[city],[state],[zip],[contract] FROM [authors]" ConnectionString="<%$ConnectionStrings:pubsConn1%>" />
0
 
LVL 18

Expert Comment

by:Elvio Lujan
ID: 16257938
in the SqlDataSourceStatusEventArgs is the property AffectedRows that have the number of rows
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16258019
I have read that but it does not work.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 16258120
You can try executing a seperate SQL command to get the total count i.e. "Select Count(ID) from TABLE". Set the count equal to a variable and that will be your total number of records. True, this makes to DB calls, but you could also execute 2 selects with one command, but then you'd have to use a data adapter and dataset to access the two command results.
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16258144
THanks,

I am aware that I can do it in SQL, but I am just flabbergasted that there is no straighforward way to count the number of records in a result generated by an SQL Statement.

I have searched the google for hours looking and for me that is too long for such a simple task, Or May Be no so simple.

Anyone else have any ideas? Or links of interest
0
 
LVL 28

Expert Comment

by:strickdd
ID: 16258234
What is the SQL statement you are currently executing?
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16258246
<asp:GridView ID="GridView1"  DataSourceID="SqlDataSource1" runat=server />
<asp:SqlDataSource ID="SqlDataSource1" runat=server SelectCommand="SELECT [au_id],[au_lname],[au_fname],[phone],[address],[city],[state],[zip],[contract] FROM [authors]" ConnectionString="<%$ConnectionStrings:pubsConn1%>" />
0
 
LVL 10

Assisted Solution

by:aki4u
aki4u earned 1000 total points
ID: 16259546
If you don't want to use second Select query then try this:

In your html gridview add:
OnRowDataBound="OnRowDataBound"

and in your code behind:
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        Context.Items["GridCount"] = Convert.ToInt32(Context.Items["GridCount"].ToString())+1;
    }

in Page_Load event add:
Context.Items.Add("GridCount", 0);


So to run this I created a label and assiged a value to it, like this(in html view of the page):
<asp:Label ID="Label1" runat="server"><%= HttpContext.Current.Items["GridCount"].ToString() %></asp:Label>
0
 
LVL 3

Accepted Solution

by:
Brentxhange earned 1000 total points
ID: 16261883
Here is a code that will allow you to count all the records, by first making the gridview disabling paging, counting the records, setting the value to lbl1(label) then re-enabling paging.
**********************************************
VB:
        GridView1.AllowPaging = False
        lbl1.Text = GridView1.Rows.Count()
        GridView1.AllowPaging = True

C# (virtually identical):
        GridView1.AllowPaging = false;
        lbl1.Text = GridView1.Rows.Count();
        GridView1.AllowPaging = true;
0
 

Expert Comment

by:Tipstar
ID: 25946417
I know this is closed but I have a followup question on the accepted solution. If i need to open an new question I will but this seems like the place to start.

I used this method (I am using the VB) and it really doesn't work for me. When  I try to change the page  i get the exception "The GridView 'MyGrid' fired event PageIndexChanging which wasn't handled."  MyGrid has  properties AllowPaging set to "True" and PageSize = "10". This problem still occurs even after I comment out the code lines to where I am not trying to change any paging property values, and have reloaded the page. In other words it is now completed broken. I have even closed the project & Visual Studio, reopened...nada will not work at all now.

 Also even though I think I am creating the label text BEFORE the paging properties are reset, the label only displays "20..." i.e. the page size, unless the number of returns is less of course.
Anyone know why and how to fix it?  thanks

With MyGrid
             .AllowPaging = False
            i = .Rows.Count()
            Label1.Text = i & " records returned"
            .AllowPaging = True
            .PageSize = 20 

        End With

Open in new window

0
 
LVL 10

Expert Comment

by:aki4u
ID: 25946680
Just wondering did you implement PageIndexChanging event for the grid?
...
aspx page...
<asp:GridView ID="MyGrid" OnPageIndexChanging="MyGrid_PageIndexChanging"  DataSourceID="SqlDataSource1" runat=server />



code behind...

Protected Sub MyGrid_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
MyGrid.PageIndex = e.NewPageIndex
MyGrid.DataBind()
End Sub

0
 

Expert Comment

by:Tipstar
ID: 25947569
OK that took care of the first part. However there are a few other issues here so I think it would be better to start a new question.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question