Display ASP.Net GridView record count inside Label on another page using Entity Framework

Rezu2215
 
on Jun 13, 2019 12:02 AM
Sample_167567.zip
2896 Views

Hello,
I want to display the count of records in a gridview in one aspx page inside a label on another aspx page using Entity Framework. Please help ASAP.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 13, 2019 01:11 AM

Hi Rezu2215,

To count GridView records and display in another page use Session.

For count GridView records you can either use GridView Rows count property or Entity set count.

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database Employees table that you can download using the link given below.

Download Northwind Database

HTML

Default

<table>
    <tr>
        <td>
            <asp:GridView runat="server" ID="gvEmployees" AutoGenerateColumns="false" AllowPaging="true"
                PageSize="5">
                <Columns>
                    <asp:BoundField DataField="EmployeeID" HeaderText="Id" />
                    <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                    <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                    <asp:BoundField DataField="Country" HeaderText="Country" />
                </Columns>
            </asp:GridView>
        </td>
        <td>
            <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" AllowPaging="true"
                PageSize="5">
                <Columns>
                    <asp:BoundField DataField="EmployeeID" HeaderText="Id" />
                    <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                    <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                    <asp:BoundField DataField="Country" HeaderText="Country" />
                </Columns>
            </asp:GridView>
        </td>
    </tr>
</table>
<br />
<asp:Button Text="Redirect" runat="server" PostBackUrl="~/Default1.aspx" />

Default1

GridView record count using Rows count is :
<asp:Label ID="lblCountRows" runat="server" />
<br />
GridView record count using Entity count is :
<asp:Label ID="lblCountEntity" runat="server" />

Code

C#

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        NorthwindModel.NorthwindEntities entities = new NorthwindModel.NorthwindEntities();
        gvEmployees.DataSource = entities.Employees.ToList();
        gvEmployees.DataBind();

        // Using GridView Rows count property.
        gvEmployees.AllowPaging = false;
        gvEmployees.DataBind();
        Session["RecordCountRows"] = gvEmployees.Rows.Count;
        gvEmployees.AllowPaging = true;
        gvEmployees.DataBind();

        // Using Entity count.
        GridView1.DataSource = entities.Employees.ToList();
        GridView1.DataBind();
        Session["RecordCountEntity"] = entities.Employees.Count();
    }
}

Default1.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    lblCountRows.Text = Session["RecordCountRows"].ToString();
    lblCountEntity.Text = Session["RecordCountEntity"].ToString();
}

VB.Net

Default.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim entities As NorthwindModel.NorthwindEntities = New NorthwindModel.NorthwindEntities()
        gvEmployees.DataSource = entities.Employees.ToList()
        gvEmployees.DataBind()

        ' Using GridView Rows count property.
        gvEmployees.AllowPaging = False
        gvEmployees.DataBind()
        Session("RecordCountRows") = gvEmployees.Rows.Count
        gvEmployees.AllowPaging = True
        gvEmployees.DataBind()

        ' Using Entity count.
        GridView1.DataSource = entities.Employees.ToList()
        GridView1.DataBind()
        Session("RecordCountEntity") = entities.Employees.Count()
    End If
End Sub

Default1.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    lblCountRows.Text = Session("RecordCountRows").ToString()
    lblCountEntity.Text = Session("RecordCountEntity").ToString()
End Sub

Screenshot

Rezu2215
 
on Jun 13, 2019 01:31 AM

Hello Sir, It shows the below error.Please help with this,

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Web.SessionState.HttpSessionState.this[string].get returned null.

Interrupted_runs.aspx.cs:

    public partial class interrupted_runs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
            
        }

        public void BindGrid()
        {
            InterruptedRuns objservice = new InterruptedRuns();
            List<Project> data = objservice.getall();
            if (data != null)
            {
                gvInterrupted.DataSource = data;
                gvInterrupted.DataBind();
                Session["RecordCountRows"] = gvInterrupted.Rows.Count;
                gvInterrupted.DataBind();
                // gvInterrupted.BottomPagerRow.Visible = true;
            }
        }
    }
}

 InterruptedRuns.cs:

namespace Vifex.DAL.Repository.Services
{
    public class InterruptedRuns
    {
        public List<Project> getall()
        {
            List<Project> file = new List<Project>();
            using (VifexDBEntities dbContext = new VifexDBEntities())
            {
                file = dbContext.Projects.Where(x=> x.Status.ToLower() == "error").ToList();
                return file;

            }
        }
    }
}

The page where count is to be shown in label:

    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
            VifexDBEntities entity = new VifexDBEntities();
            var result = (from proj in entity.Projects
                          select proj);
            lblTotal.Text = result.Count().ToString();
            //var projectProjectRunning = "SELECT * FROM [InterruptedRuns]";
            //var projectProjectInterrupted = "SELECT * FROM [gvInterrupted]";
            //lblrunning.Text = projectProjectRunning.Count().ToString();
            lblInterrupted.Text = Session["RecordCountRows"].ToString();
        }
        public void BindGrid()
        {
            DashboardService getdata = new DashboardService();
            var project = getdata.getall();
            DashboardGV.DataSource = project;
            DashboardGV.DataBind();
        }   

Please tell me where is  the value assigned to session.

dharmendr
 
on Jun 14, 2019 03:06 AM

Then the problem is before the GridView gvInterrupted populated your Dashboard page runs.

The sample code which i provided is correct.

Based on the your project flow you need to change the code accordingly.