Click to See Complete Forum and Search --> : Writing a collection class


ivanjay205
07-08-2010, 10:56 PM
Hi all,

I am writing the login piece to a new web app I am writing. i have a database that maintains my a table of my company's Employees. This will include their hr data along with their login data and role permissions.

I have the DAL portion to read individual employees.

However, I need to create an employeeList class which is essentially a collection of all of the employees in the database. I am assuming collection class is the right way to go here....

I have been googling collection classes and looking through my books and cant find specifically what I am looking for. i am finding a lot of references to the generic collection classes .net provides but I am assuming I need to create a collection of my specific class (employee).

How would I go about this?

Thanks!

ryanbutler
07-09-2010, 04:03 PM
If I understand correctly, which I'm not sure that I do, you could create an employee class, issue a stored procedure to grab all employees, and then loop through creating a new object for each employee, and then adding that object each time to a generic list object that's typed to your class and just return the list object to whatever page you need.


namespace EmployeeDirectory
{
public class EmployeeDirectory
{
public List<EmployeeDirectory>GetEmployeesView(string lastname)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mwd"].ConnectionString);
SqlCommand cmd = new SqlCommand("spEmployeeDirectoryViewDetail", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameterLName = new SqlParameter("@LName", SqlDbType.VarChar,50);
parameterLName.Value = lastname;
cmd.Parameters.Add(parameterLName);
List<EmployeeDirectory> employees = new List<EmployeeDirectory>();
try
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
EmployeeDirectory ed = new EmployeeDirectory();
ed.name = rdr["Name"].ToString();
ed.guid = rdr["Guid"].ToString();
employees.Add(ed);
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
cmd.Dispose();
conn.Close();
}
return employees;

}
}
}