Use of Datatable Select method.

Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 1041
Suppose, we want to process individual user records so we can not write a query again and again and poll to database. It's time taking process.

So, to reduce it, what we will do,

We will fetch all records, Suppose, we have a table called 'employee master' containing huge data in it.
So, we will store in DataTable at once, meaning writing a below query

On the Page Load
Select * from Employee_Master order by emp_name;

ViewState["Employee_data"] = dt_employee;

//here dt_employee is your Datatable object which contains all Employee records.

Now, use Select method,

Suppose we want record of a particular Employee,then
in any events, where you want to populate particular Employee record, write

DataTable dt_emp = Ctype(ViewState["Employee_data"],DataTable);
//Here Ctype is used for casting purpose, you can also use DirectCast instead on

//declare DataRow array as
DataRow[] dr_employee = dt_emp.Select("emp_id='"& pass_employee_id &"'");

//Where emp_id is your column name and pass_employee_id is variable in which you will pass particular Employee-Id

Note: Select method returns always Array of DataRow

//Check if dr_employee is null or it's Length is 0 as

//Now, take another DataTable as
DataTable dt_new_employee = null;

dt_new_employee = dr_employee.CopyToDataTable();

//Note: CopyToDataTable will give you datatable.

dtl_view_employee_details.DataSource = dt_new_employee;

So, here you learn Select method, it's useful for filtering/search criteria.
for this, you do not need to go to Database and fetch single row.

Comments or Responses

Login to post response