Generally we use the Dataset to store the data in .Net, but dataset takes more memory as compared to Entity class and developer cannot know underlying data type in dataset, but using entity class we can find data type easily.
In this article we discuss about how can we convert dataset datatable into entity object. First we create the sample Employee entity class for XML datasource Employees.xml file as shown below.
using System;
///<summary>
/// Employee Entity Class
///</summary>
public class Employee
{
public string Id { get; set; }
public string Name { get; set; }
public string Salary { get; set; }
}
<?xmlversion="1.0"encoding="utf-8" ?>
<Employees>
<Employee>
<Id>1</Id>
<Name>Smith</Name>
<Salary>10000</Salary>
</Employee>
</Employees>
Now we will read the xml file by using dataset and will convert dataset object into Entity class object for easy transportation as a light weight object as shown below.
using System;
using System.Data;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
try
{
DataSet dsEmp = new DataSet();
dsEmp.ReadXml(Server.MapPath("Employees.xml"));
DataTable dtEmp = dsEmp.Tables[0];
Employee objEmp = new Employee();
foreach (PropertyInfo prop in objEmp.GetType().GetProperties())
foreach (DataColumn dc in dtEmp.Columns)
if (prop.Name == dc.ColumnName && prop.CanWrite)
if (dtEmp.Rows[0][dc.ColumnName] != DBNull.Value)
prop.SetValue(objEmp, dtEmp.Rows[0][dc.ColumnName], null);
txtId.Text = objEmp.Id.ToString();
txtName.Text = objEmp.ToString();
txtSal.Text = objEmp.Salary.ToString();
}
catch (Exception ex)
{ }
}
}
Here we use the System.Reflection namespace to convert datatable columns into entity class properties. Using PropertyInfo class we can create data column into entity class property. Use SetValue() method of PropertyInfo class to set the value for the property.
In the above example we are displaying datatable data through entity object if the datatable has only one row. If the datatable has more than one row, we can convert datatable into entity list by using System.Collections.Generic namespace as shown below.
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
try
{
DataSet dsEmp = new DataSet();
dsEmp.ReadXml(Server.MapPath("Employees.xml"));
DataTable dtEmp = dsEmp.Tables[0];
List<Employee> listEmp=new List<Employee>();
Employee objEmp = new Employee();
for (int row = 0; row < dtEmp.Rows.Count; row++)
{
foreach (PropertyInfo prop in objEmp.GetType().GetProperties())
foreach (DataColumn dc in dtEmp.Columns)
if (prop.Name == dc.ColumnName && prop.CanWrite)
if (dtEmp.Rows[0][dc.ColumnName] != DBNull.Value)
prop.SetValue(objEmp, dtEmp.Rows[0][dc.ColumnName], null);
listEmp.Add(objEmp);
}
}
catch (Exception ex)
{ }
}
}