In different ways, dataadapter.Fill() method can be overridden. In this article, i have tried to elaborate this. Here all the 5 different ways has been explained with code and snap shots.
5 different overloads of Dataadapter.Fill() Method
As i have already stated in the following , link http://www.dotnetfunda.com/articles/article1146-data-adapter-.aspx
, that Dataadapter is working just like a bridge between a datasource and dataset. One of the method of Dataadapter is Fill() Method.
It can be overloaded in 5 different ways.
I have tried here to clearly explain, how this Fill() method can be used in 5 different ways.
Dataadapter.Fill(ds) here fetches the data from tblMobile
and fills in the DataSet ds
On clicking the button Fill DataSet, the dataadapter fills the dataset and finally, the GridView shows all the records present in the dataset ds.
Here Dataadapter.Fill(dt) fetches records from tblMobile and fills in the datatable named dt.
On clicking the button Fill DataTable, the dataadapter fills the datatable and finally Gridview displays all the records present in the datatable.
Here a datatable named nameOfDataTable has been filled by the dataadapter.Fill() method. As this datatable is within the dataset, we can use the commented line also to fill the gridview. Let me be clear here,
To set the datasource of the gridview control we can use:
- ds.Tables //valid
- ds.Tables["nameOfDataTable"] //valid
As this is the first table of the dataset, so we can use ds.Tables, otherwise we can use the table name to indicate which table i need to access.
This overload takes 3 parameters. The types of parameters are int,int and datatable. Here first parameter is the starting record, second parameter is the maximum number of record and the last parameter is the datatable where we have to fill the records. The range from which the records are to be fetched are mentioned here using the parameters.
At last, this overload takes 4 parameters. The types of parameters are dataset,int,int,string. The first parameter is the name of the dataset, second parameter is the starting record number, third parameter is the maximum number of records and the last parameter is the name of the table.
At first ds1 is filled with all the records of tblMobile. And then, dataadapter.Fill() method has filled the datatable nm with the specified range of records.
Following figure shows the contents of dataset ds1. It is having all the records of tblMobile.
Now, the dataadapter object da1 takes 4 parameters and fills the datatable named nm with the records of specified range.
Hope this article was useful, thanks
Happy reading !