Bind (Populate) ListView using Business class with Entity Framework in ASP.Net

nauna
 
on Jan 20, 2021 10:46 PM
Sample_119991.zip
717 Views

hello

i have query. i used this on many pages and all the time i have to write it.

How to make class file so i can just call the method and i can bind with Listview on page.

var q=(from c in db.country
select c);
ListView1.DataSource=q.List();
ListView1.Databind();

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 21, 2021 03:33 AM

Hi nauna,

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

Database

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

Download Northwind Database

HTML

<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
    ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
    <LayoutTemplate>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>CustomerId</th>
                <th>ContactName</th>
                <th>Country</th>
            </tr>
            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
            <tr>
                <td colspan="3">
                    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="10">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                                ShowNextPageButton="false" />
                            <asp:NumericPagerField ButtonType="Link" />
                            <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
                        </Fields>
                    </asp:DataPager>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>
    </GroupTemplate>
    <ItemTemplate>
        <td><%# Eval("CustomerId") %></td>
        <td><%# Eval("ContactName") %></td>
        <td><%# Eval("Country") %></td>
    </ItemTemplate>
</asp:ListView>

DataAccess

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class DataAccess
{
    public IEnumerable<Customer> GetData()
    {
        IEnumerable<Customer> customers;
        using (NorthwindEntities entity = new NorthwindEntities())
        {
            customers = (from c in entity.Customers
                         select c).ToList();
        }

        return customers;
    }
}

VB.Net

Imports Microsoft.VisualBasic

Public Class DataAccess
    Public Function GetData() As IEnumerable(Of Customer)
        Dim customers As IEnumerable(Of Customer)
        Using entity As NorthwindEntities = New NorthwindEntities()
            customers = (From c In entity.Customers Select c).ToList()
        End Using

        Return customers
    End Function
End Class

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindListView();
    }
}

protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    this.BindListView();
}

private void BindListView()
{
    DataAccess dataAccess = new DataAccess();
    lvCustomers.DataSource = dataAccess.GetData().ToList();
    lvCustomers.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindListView()
    End If
End Sub

Protected Sub OnPagePropertiesChanging(ByVal sender As Object, ByVal e As PagePropertiesChangingEventArgs)
    TryCast(lvCustomers.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
    Me.BindListView()
End Sub

Private Sub BindListView()
    Dim dataAccess As DataAccess = New DataAccess()
    lvCustomers.DataSource = dataAccess.GetData().ToList()
    lvCustomers.DataBind()
End Sub

Screenshot