Click here to Skip to main content
15,388,904 members
Articles / Web Development / ASP.NET
Posted 18 Jan 2013


2 bookmarked

How to Copy Data from SqlDataSource to Datatable and Operations Using Datatable

Rate me:
Please Sign up or sign in to vote.
4.80/5 (3 votes)
18 Jan 2013CPOL
I'll Try To Explain Some Methods of Datatables.


I wanted to share my experience where the project required with the use of datatables to store data intermediate without moving the data to the database and  required to perform some operations. For ex: Selecting some specified rows and computing some arithmetic operations.   


Datatable is an object in the ADO.NET Library. Generally, Dataset and Dataview uses the Datatable. Datatable helps in the sorting and Selecting the data present in it by using its methods.

Using the code 

Copying Data From SqlDataSource To Datatable 

There is no way to perform direct copy from sqldatasource to datatable. Therefore, we use DataView to copy the data from sqldatasource to datatable.
DataView dv = (DataView)sqlDS1.Select(DataSourceSelectArguments.Empty);
DataTable dt = new DataTable();
dt = dv.ToTable();  
Sorting Data in Datatable 

DataTable.Select() is a method which helps in sorting the data. The  select() has 4 overloads. According, to My project  requirement i am in need of 2 overloads. It Returns the Array Of the DataRow[] Sorted. 

The DataTable.Select() Works exactly as same as the 'where'  Clause in SQL. I.e,

1. DataTable.Select("Filter Expression")  

DataTable dtr = dt;
DataRow[] uniname = dtr.Select("Name Desc");    

 2. DataTable.Select("Filter Expression","Sorting Order")  

DataTable dtr = dt;
DataRow[] uniname = dtr.Select("City=Bangalore","Name Desc");  

Filtering Data in Datatable  

DataTable.Compute() is a method which helps in filtering the data. The DataTable.Compute("Required Expression","Filter")

DataTable dtr = dt;
Object Sum = dtr.Compute("Sum(Salary)","date > 1/1/12 and date < 1/1/13 and ID=1 ");   


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nandakishore G N
Software Developer (Senior) Dhruv Compusoft Consultancy Pvt Ltd
India India
Software Engineer in Canarys Automation Pvt Ltd, Bangalore, India.

My Blog

Comments and Discussions

Questionc# Pin
Manohar_manu18-Jan-13 16:40
MemberManohar_manu18-Jan-13 16:40 
good tip..nice one..
AnswerRe: c# Pin
vinodkumarnie18-Jan-13 16:53
Membervinodkumarnie18-Jan-13 16:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.