Display records count from Database using Entity Framework in ASP.Net

Last Reply 6 days ago By pandeyism

Posted 6 days ago

Hello,

I want to display the number of records present in my database in 3 different labels based on their status whether Running , Total records or Finished. I am using edmx for database. Please help ASAP. 

My database table contains entries as ID, Name and Status.

Please reply soon.

Posted 6 days ago Modified on 6 days ago

Hi Rezu2215,

Refer below sample.

Database

For this sample I have used of NorthWind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:Label id="lblCount" runat="server" />
<br />
<asp:Label id="lblCountryCountUSA" runat="server" />
<br />
<asp:Label id="lblCountryCountUK" runat="server" />

Namespaces

C#

using NorthwindModel;

VB.Net

Imports NorthwindModel

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    NorthwindEntities entity = new NorthwindEntities();
    var result = (from emp in entity.Employees
                    select emp);
    var countryCountUSA = (from emp in entity.Employees
                            select emp).Where(x => x.Country == "USA");
    var countryCountUK = (from emp in entity.Employees
                            select emp).Where(x => x.Country == "UK");
    lblCount.Text = "Total Number of Records : " + result.Count().ToString();
    lblCountryCountUSA.Text = "USA Record Count is  : " + countryCountUSA.Count().ToString();
    lblCountryCountUK.Text = "UK Record Count is :" + countryCountUK.Count().ToString();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim entity As NorthwindEntities = New NorthwindEntities()
    Dim result = (From emp In entity.Employees Select emp)
    Dim countryCountUSA = (From emp In entity.Employees Select emp).Where(Function(x) x.Country = "USA")
    Dim countryCountUK = (From emp In entity.Employees Select emp).Where(Function(x) x.Country = "UK")
    lblCount.Text = "Total Number of Records : " & result.Count().ToString()
    lblCountryCountUSA.Text = "USA Record Count is  : " & countryCountUSA.Count().ToString()
    lblCountryCountUK.Text = "UK Record Count is :" & countryCountUK.Count().ToString()
End Sub

Output

Total Number of Records : 9
USA Record Count is : 5
UK Record Count is :4