Learn How to Data Bind a Custom Business Object in an ASP.NET 3.5 Web App C#

Category: .NET Framework

Data Binding to a Custom Business Object

This tutorial shows you how to data bind a custom business object in an ASP.NET 3.5 Web Application. C#.

In this tutorial you will learn how to create a component that can return data to a Web page. This component will use an XML file for its data. You will also learn how to reference the business object as a data source on a Web page and bind a control to the data returned by the business object. Finally you will learn how read and write data by using the business object.

Here is a snapshot of what the Web site will look like:

To start, we will open Visual Web Developer, and click File > New Web Site. Then click under the Visual Studio installed templates > ASP.NET Web Site. Choose a location and enter the name of the folder where you want to keep the pages of this Web site. Do not forget that in the language list to click on the programming language you desire to develop in. For this example we will use C#. Then click OK.

After Visual Web Developer creates the folder and a new page named Default.aspx, we want to create an XML file. Simply go to the Solution Explorer, Right-click App_Data > then choose Add New Item.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

**It is important to make sure that you create the XML file in the App_Data folder, because the folder has permissions set on it that will allow the Web page to read and write data to the XML file.

Now under Visual Studio installed templates click > XML file. Then in the name box, type Employee.xml. Then click Add. Now you have just created a new XML file that contains the only XML directive. Here is an example of what your XML file might contain:

808 429-7886

870 625-8897


Ok. Now we have an XML file called Employee.xml. Next we need to create a class to act as your business component. This component needs to be kept in the App_Code folder of our Web site. In a real world app, this can be kept in any convenient store, including a global assembly cache.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Furthermore, let’s create a directory called App_Code if you do not already have one. This is pretty easy. In the Solution Explorer, simply Right-click > name of your Web site, click > Add ASP.NET Folder click > App_Code. **This folder must be named App_Code. Once that is complete it is time to add the component to your site!

Now we need to create a business component. In order to do this we need to make sure that you Right-click > App_Code folder within the Solution Explorer, then click > Add New Item. An Add New Item Box should appear. Under the Visual Studio templates, click > Class, and rename it BusinessObject, then click > Add. Visual Web Developer creates the new class file and opens the code editor. Here is some example code for a business component:

using System;
using System.Web;
using System.Data;
namespace XMLClasses
public class EmployeeClass
private DataSet dsEmployee = new DataSet("ds1");
private string filePath = HttpContext.Current.Server.MapPath("~/App_Data/Employee.xml");

public EmployeeClass()
dsEmployee.ReadXml(filePath, XmlReadMode.ReadSchema);

public DataSet GetEmployee()
return dsEmployee;

Make sure that you change the filepath variable to reference your XML file name that you just created. Then save file.

Next we are going to display the data by using the business component. In order to create an ObjectDataSource control that references the component, switch or open the default.aspx page. If you do not have a default.aspx page you can create one. Right-click on the name of your Web site in the Solution Explorer, then click > Add New Item, and then add a Web Form.

Now we are going to switch to Design View. From the Toolbox, inside the Data folder, drag an XMLDataSource control onto the page. Located in the properties window; simply set ID to EmployeeObjectDataSource. Then Right-click > ObjectDataSource control, locate and click > smart tag to display the ObjectDataSource Tasks menu.

On the ObjectDataSource Tasks menu, click > Configure Data Source. The Configure Data Source wizard should appear. Choose in your business object list, click > PubsClasses.EmployeeClass; then click Next. Navigate to the App_Data file and choose Employee.xml then click > Finish.

In order to display the data from the component navigate to the Toolbox, locate the Data folder, and drag a GridView control onto the page. If the Common GridView Tasks menu is not showing, Right-click > GridView control and click the smart tag. Otherwise, on the Common GridView Tasks menu, Choose Data Source box, click > EmployeeXMLDataSource. Save File and Run. The GridView with the XML data in it should be displayed.

We moved our Web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

In order to modify the business component to allow inserts, simply switch to the BusinessObject file, and add this method as a final member of the EmployeeClass.

public void InsertEmployee(string strEmp_id, string strEmp_lname, string strEmp_fname, string strEmp_phone)
DataRow workRow = dsEmployee.Tables[0].NewRow();
workRow[0] = strEmp_id;
workRow[1] = strEmp_lname;
workRow[2] = strEmp_fname;
workRow[3] = strEmp_phone;
dsEmployee.WriteXml(filePath, XmlWriteMode.WriteSchema);

This method takes four values to insert, which you will need to provide in the page parameters. Please pay close attention to the names of the variables used to pass employee info into the method(emp_id, emp_lname, emp_fname, emp_phone). These must match the column names defined in the schema of the XML file you created. Save.

Finally, to add a control for inserting data quickly switch to or open the Default.apsx and switch to design view. From the Toolbox, locate the Data folder, drag DetailsView control onto the page. Do not worry about the layout.

Ok now from the DetailsView’s Tasks menu, Choose Data Source box, click > EmployeeObjectdataSource. Then locate the properties window and change AutoGenerateInsertButton to true. This render’s a New button that users can click to put in a new entry. Now you can insert new employees into the XML file.

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!

100% SPAM FREE! We will never sell or rent your email address!