Click here to Skip to main content
Click here to Skip to main content

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

, 18 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Nandakishore G N
Software Developer Canarys Automation Pvt Ltd
India India
Software Engineer in Canarys Automation Pvt Ltd, Bangalore, India.
 
My Blog gnnrao.blogspot.in

Comments and Discussions

 
Questionc# PingroupManohar_manu18-Jan-13 16:40 
AnswerRe: c# Pinmembervinodkumarnie18-Jan-13 16:53 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141029.1 | Last Updated 18 Jan 2013
Article Copyright 2013 by Nandakishore G N
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid