In this article, I’ll explain how to make use of Custom Business Objects to bind data to Databound controls like the ASP.Net GridView Control.
Database
I’ll be making use of the Customers table of the Northwind Database. You can download the Northwind database using the link given below.
Download Northwind Database
Connection String
I am making use of SQL Server 2005 Express Edition and below is the connection string to the database.
<addname ="constring"
connectionString="Data Source = .\SQLExpress; Initial Catalog = Northwind;
Integrated Security = true"/>
ASP.Net GridView Control
Below is the ASP.Net GridView Control that will be used in this article to bind the Custom Business Objects.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns = "false"
AllowPaging = "true" OnPageIndexChanging = "OnPaging">
<Columns>
<asp:BoundField DataField = "CustomerId" HeaderText = "Customer Id" />
<asp:BoundField DataField = "ContactName" HeaderText = "Contact Name" />
<asp:BoundField DataField = "City" HeaderText = "City" />
<asp:BoundField DataField = "Country" HeaderText = "Country" />
</Columns>
</asp:GridView>
Business Object Class
We’ll now create a Business Object Class or Properties class called Customers that will hold the data that we want to fetch from the database.
C#
public class Customer
{
string _customerId;
public string CustomerId
{
get
{
return _customerId;
}
set
{
_customerId = value;
}
}
string _contactName;
public string ContactName
{
get
{
return _contactName;
}
set
{
_contactName = value;
}
}
string _city;
public string City
{
get
{
return _city;
}
set
{
_city = value;
}
}
string _country;
public string Country
{
get
{
return _country;
}
set
{
_country = value;
}
}
}
VB.Net
Public Class Customer
Private _customerId As String
Private _contactName As String
Private _city As String
Private _country As String
Public Property CustomerId() As String
Get
Return _customerId
End Get
Set(ByVal value As String)
_customerId = value
End Set
End Property
Public Property ContactName() As String
Get
Return _contactName
End Get
Set(ByVal value As String)
_contactName = value
End Set
End Property
Public Property City() As String
Get
Return _city
End Get
Set(ByVal value As String)
_city = value
End Set
End Property
Public Property Country() As String
Get
Return _country
End Get
Set(ByVal value As String)
_country = value
End Set
End Property
End Class
Populating the Custom Business Objects
Once our Custom Business Object class or Properties class is created, we can build a method that will populate the Custom Business Object Collection with the data that is fetched from the database.
The function below fires a select query on the Customers table of the Northwind database and populates the fetched records in the Custom Business Object’s List Collection using SQL Data Reader and returns the Custom Business Object’s collection.
C#
private List<Customer> PopulateData()
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("select * from customers", con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
List<Customer> customers = new List<Customer>();
while (sdr.Read())
{
customers.Add(new Customer());
customers[customers.Count - 1].CustomerId = sdr["CustomerID"].ToString();
customers[customers.Count - 1].ContactName = sdr["ContactName"].ToString();
customers[customers.Count - 1].City = sdr["City"].ToString();
customers[customers.Count - 1].Country = sdr["Country"].ToString();
}
con.Close();
return customers;
}
}
}
}
VB.Net
Private Function PopulateData() As List(Of Customer)
Dim constring As String = ConfigurationManager.ConnectionStrings("constring").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("select * from customers", con)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader
Dim customers As New List(Of Customer)
While sdr.Read
customers.Add(New Customer)
customers((customers.Count - 1)).CustomerId = sdr("CustomerID").ToString
customers((customers.Count - 1)).ContactName = sdr("ContactName").ToString
customers((customers.Count - 1)).City = sdr("City").ToString
customers((customers.Count - 1)).Country = sdr("Country").ToString
End While
con.Close()
Return customers
End Using
End Using
End Using
End Function
Binding the ASP.Net GridView Control
Once Custom Business Object Collection is populated we can bind the same to the ASP.Net GridView Control. The function below calls the PopulateData method and then binds the populated collection to the ASP.Net GridView Control.
C#
private void BindGrid(List<Customer> customers)
{
gvCustomers.DataSource = customers;
gvCustomers.DataBind();
}
VB.Net
Private Sub BindGrid(ByVal customers As List(Of Customer))
gvCustomers.DataSource = customers
gvCustomers.DataBind()
End Sub
The above method is called in the Page Load event of the ASP.Net Web Page in the following way.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid(this.PopulateData());
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid(Me.PopulateData)
End If
End Sub
Pagination
Below is the Event Handler to handle Pagination in the ASP.Net GridView Control
C#
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
this.BindGrid(this.PopulateData());
}
VB.Net
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Me.BindGrid(Me.PopulateData)
End Sub
That’s it. So here we come to an end of this article that explains us on how to Bind the Databound controls like GridView with Custom Business Objects, same way you can bind other controls like DataList, Repeater, etc. You can download the sample source code in VB.Net and C# using the link below.
BindingCustomBusinessObjectsToGridView_VB.zip
BindingCustomBusinessObjectsToGridView_CS.zip