Click here to Skip to main content
15,867,914 members
Articles / Web Development / ASP.NET
Tip/Trick

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 38.2K   2   2
I'll Try To Explain Some Methods of Datatables.

Introduction

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.   

Background  

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 ");   

License

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


Written By
Software Developer (Senior) Dhruv Compusoft Consultancy Pvt Ltd
India India
Senior MES Developer in TruGlobal Pvt Ltd, Bangalore, India.

My Blog
dotnetkraft.blogspot.in
gnnrao.blogspot.in

Comments and Discussions

 
Questionc# Pin
Manohar_manu18-Jan-13 16:40
Manohar_manu18-Jan-13 16:40 
AnswerRe: c# Pin
vinodkumarnie18-Jan-13 16:53
vinodkumarnie18-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.