DepartmentID = d.DepartmentID";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
employeeList = new Employees();
while (dr.Read())
{
Employee emp = new Employee();
if (dr["EmpCode"] != DBNull.Value)
emp.EmpCode = dr["EmpCode"].ToString();
if (dr["EmpName"] != DBNull.Value)
emp.EmpName = dr["EmpName"].ToString();
if (dr["Salary"] != DBNull.Value)
emp.Basic =
Convert.ToDouble(dr["Salary"].ToString());
if (dr["DeptCode"] != DBNull.Value)
emp.DeptCode = dr["DeptCode"].ToString();
if (dr[?«DeptName?»] != DBNull.Value)
emp.DeptName = dr[?«DeptName?»].ToString();
employeeList.Add(emp);
emp = null;
}
}
catch
{
throw;
}
finally
{
conn.Close();
}
return employeeList;
}
Working with LINQ
[ 228 ]
Note that the GetAllEmployees() method returns an instance of Employees class.
The Employees class is actually List type and comprises of a collection of Employee
instances. In the Page_Load event of the web form, write the following code:
DataManager dataManager = new DataManager();
GridView1.DataSource = from emp in dataManager.GetAllEmployees()
where emp.Basic > 10000
select new
{
emp.EmpCode,
emp.EmpName,
emp.Basic
};
GridView1.DataBind();
Note that we have used the Where clause to restrict the display. Only those
employees whose Basic is greater then 10000 will be retrieved by the LINQ statement
and the result set bound to the GridView control.
Pages:
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212