Hi iammann,
Here is a small sample that you can understood the concept.
HTML
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:GridView ID="gvEmployees" runat="server" Caption="<b>Interface</b>" />
</td>
<td>
<asp:GridView ID="gvCustomers" runat="server" Caption="<b>Abstract</b>" />
</td>
</tr>
</table>
</div>
For interface create a property class with the name Employees
/// <summary>
/// Employees get set property.
/// </summary>
public class Employees
{
public string EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then create interface IEmployee
/// <summary>
/// interface IEmployee.
/// </summary>
public interface IEmployee
{
/// <summary>
/// Get list of Employees.
/// </summary>
/// <returns>List of Employees</returns>
List<Employees> PopulateEmployees();
}
Then create class Employee which inherit interface.
/// <summary>
/// Employee Class inherits IEmployee interface.
/// </summary>
public class Employee : IEmployee
{
/// <summary>
/// Get list of Employees.
/// </summary>
/// <returns>List of Employees</returns>
public List<Employees> PopulateEmployees()
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string query = "SELECT TOP 10 [EmployeeID],[LastName],[FirstName] FROM [Employees]";
SqlCommand cmd = new SqlCommand(query);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.Connection = con;
DataTable dt = new DataTable();
sda.Fill(dt);
List<Employees> employees = new List<Employees>();
foreach (DataRow row in dt.Rows)
{
employees.Add(new Employees
{
EmployeeId = row["EmployeeId"].ToString(),
FirstName = row["FirstName"].ToString(),
LastName = row["LastName"].ToString()
});
}
return employees;
}
}
For abstract same way create class with the name Customer
/// <summary>
/// Summary description for Customer
/// </summary>
public abstract class Customer
{
/// <summary>
/// Gets List of Customers.
/// </summary>
/// <returns>List of Customers.</returns>
abstract public DataTable PopulateCustomers();
}
Create Customers class that override Customer abstract class.
/// <summary>
/// Summary description for Customers
/// </summary>
public class Customers : Customer
{
/// <summary>
/// Override PopulateCustomers.
/// </summary>
public override DataTable PopulateCustomers()
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string query = "SELECT TOP 10 [CustomerID],[ContactName],[City],[Country] FROM [Customers]";
SqlCommand cmd = new SqlCommand(query);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.Connection = con;
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
After running the above below is the output.
Interface
EmployeeId | FirstName | LastName |
1 |
Nancy |
Davolio |
2 |
Andrew |
Fuller |
3 |
Janet |
Leverling |
4 |
Margaret |
Peacock |
5 |
Steven |
Buchanan |
6 |
Michael |
Suyama |
7 |
Robert |
King |
8 |
Laura |
Callahan |
9 |
Anne |
Dodsworth |
18 |
Shashikant |
Moria |
|
Abstract
CustomerID | ContactName | City | Country |
AAFKM |
Mudassar Khan |
Warszawa |
Belgium |
ALFKI |
Maria |
Boise |
Austria |
ANATR |
Ana Trujillo |
México D.F. |
France |
ANTON |
Antonio Moreno |
Montréal |
Brazil |
AROUT |
Thomas Hardy |
Mannheim |
Ireland |
BERGS |
Christina Berglund |
Luleå |
Italy |
BLAUS |
Hanna Moos |
Mannheim |
Finland |
BLONP |
Frédérique Citeaux |
Strasbourg |
Finland |
BOLID |
Martín Sommer |
Madrid |
Argentina |
BONAP |
Laurence Lebihan |
Marseille |
USA |
|