This is the code I have:

public class BusinessLayer
{
    public Employee[] GetAllEmployees() 
    {
        Employee[] employees;
        DataSet myDataSet = DataBaseLayer.GetAllEmployees();

      /// How can I convert the myDataSet to Employee List
      return employees;
   }     
}

Recommended Answers

All 3 Replies

More information is needed:

1) structure of Employee
2) structure/contents of the DataSet returned by: DataBaseLayer.GetAllEmployees();

More information is needed:

1) structure of Employee
2) structure/contents of the DataSet returned by: DataBaseLayer.GetAllEmployees();

I am learning a new concept of programming, the right one (data access layer, business logic layer, and presentation layer)

For DAL I did:

public class DataBaseLayer {
   public static DataSet GetAllEmployees() {
      /// Sql Connection
      /// Sql Command
      /// Sql Reader

      return empDataSet;
   }

}

And how I am doing on BLL (the code from the 1st post)

Yes, that's good that you are separating the code. However, in order to tell you how to convert a DataSet or rather your empDataSet to populate your Employee [] employees , I still need to know how each is defined.

For the "empDataSet", I assume that contains records from an Employee table or something, but I don't know what the table columns are...

For the Employee type, I assume it contains members that represent the DB columns from 'Employee' table, but I don't know what these fields are either...

I would like to mention though, that if your DAL is populating the DataSet with records that will match the structure of your Employee type, you might achieve the end result without any needed conversion at all between the DAL and the BLL because if your BLL expects a table containing columns with particular names, you can simply use the returned DataSet's Tables[0], which is a DataTable that can be accessed by column name such as: empDataSet.Tables[0].Rows[0]["Lastname"]; , which would give you the table's 1st row value for column "Lastname" if that is what it is called. Of course this code should be broken out to make it less cryptic, but I just wanted to demonstrate that you might not need to do any conversion from DataSet to a separate Employee [] type.


Example of breaking down the empDataSet.Tables[0].Rows[0]["Lastname"]; mention above:

// Retrieve reference to the Employee table from DataSet...
                DataTable dt = empDataSet.Tables[0];

                // Process each employee record in table...
                foreach (DataRow dr in dt.Rows)
                {
                    string lastName = (string)dr["Lastname"];
                    string firstName = (string)dr["Firstname"];
                }

So, if the your BLL's GetAllEmployees method expects to process employee records from the DAL, you can just reference those records from the DataTable as mentioned above, thus avoiding any need to perform any conversion to an Employee[]--unless you have a real need to do that.;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.