*/
Stuck? Need help? Ask questions on our forums.

Other Views

corner
*/

ADONET FAQ - Getting the Result and Filling It to the Dataset

How do I get the result of my command and fill it to the dataset?

DataSet is a local and offline container of the data. The DataSet object is created simply like

C# Version
DataSet ds = new DataSet();


VB.Net Version
Dim ds As New DataSet()


Now we need to fill the DataSet with the result of the query. We will use the dataAdapter object for this purpose and call its Fill() method. This is the step where data adapter connects to the physical database and fetch the result of the query.

C# Version
dataAdapter.Fill(ds, "prog");


VB.Net Version
da.Fill(ds, "prog")


Here we have called the Fill() method of dataAdapter object. We have supplied it the dataset to fill and the name of the table (DataTable) in which the result of query is filled.

This is all we needed to connect and fetch the data from the database. Now the result of query is stored in the dataset object in the prog table which is an instance of DataTable. We can get a reference to this table by using the indexer property of dataset object’s Tables collection

C# Version
DataTable dataTable = ds.Tables["prog"];


VB.Net Version
Dim dataTable As DataTable
dt = ds.Tables("prog")


The indexer we have used takes the name of the table in dataset and returns the corresponding DataTable object. Now we can use the tables Rows and Columns collection to access the data in the table.

Back
corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.