Updated April 17, 2023
Introduction to Dataset in ASP.NET
Dataset in ASP.NET is providing the representation of disconnected results from the data source, and this result is completely separated from the data source. The ASP.NET data set is working with the data without knowing any source which was coming from, dataset provides the more flexibility when working with a result set of data. ASP.NET dataset is giving the more advantages as compared to the data reader because the data reader in ASP.NET is working only the connection-oriented data sources. Dataset in ASP.NET is nothing but the container of one or more data tables object which contains data of the database.
What is Dataset in ASP.NET?
- Dataset in ASP.NET is nothing but the container of one or more objects of the data table which contained the data retrieved from the database.
- We can also set the data related to the object of the data table within a single dataset. The data adapter object is allowing to populate the data tables into the dataset. We can also use the data adapter fill method to populate the data in the dataset.
- The dataset in ASP.NET can be filled dynamically or by using a data source.
- Dataset in ASP.NET contains the columns, primary key, rows, data table object relations and constraints.
- We can say that ASP.NET is the representation of memory-resident of data which was providing a consistent relational programming model.
- We have also using the dataset sqladapter class with a combination of datasets. The sqladapter class in ASP.NET is allowing us to populate the dataset in data tables.
- ASP.NET dataset is a collection of data tables which contained the data. Dataset is used to fetch the data without interacting with any data source.
- It is nothing but the in-memory data store and at one time it was holding more than one table. We can also use asp.net dataset to write and read the data from documents of XML.
- ASP.NET is providing the class of dataset which was used to create the dataset object. It was also used to perform the data related operation on methods and constructors.
- In ASP.NET dataset is representing the subset of memory in the database. Which means asp.net is nothing but a collection of table’s data which contains the relational data into the tabular format.
- ASP.NET dataset requires a continuously active and open connection into the database server. Also, we can say that the dataset is based on the disconnected architecture.
- We can fetch the data from the dataset without interacting with any other data source.
How to Use Dataset in ASP.NET?
We can use the dataset as a container in ASP.NET, it can be data objects which contains the data which we are retrieving from the database. Datasets are the part of the layer which was accessing the data.
Below are the steps which show how to use the dataset in ASP.NET.
- Setup and declare the connection string
- Build and declare the connection string
- Create the SQL data adapter object
- Fill in the data and create the dataset object
- Bind our dataset with the DataGridView
1. Setup the connection string
- To use the dataset in our application first step is to set up the connection string. This task will be declared and instantiates the connection with the database.
- We need a connection string when we have accessed the database server. We have using the sql connection class to connect the MSSQL server database.
2. Build and declare the string of query
- All the query statement which was used in the specified program which was declared in this step.
3. Create SQL data adapter object
- The data adapter is basically used to retrieve the data from the database server. Data adapter is nothing but the connector class which sat between disconnected and the connected parts.
- Basically, the data adapter class is instantiated with the new constructor.
4. Fill the account data and create dataset object
- In this step we are declaring the dataset object in our project we have declaring the object as below:
aspDataSet = new DataSet()
- After setting the dataset and adapter we have to populate the dataset. Fill method data adapter class is used to refresh and populate the object of the dataset.
5. Attach dataset to the DataGridView
- In this step, we are using the datasource method for the DataGridView control which was used to attach the dataset.
Find Table Dataset in ASP.NET / Row Count
Basically, the data table collection contains the one or more objects of the data table. Data table also contains the zero objects of the data table. Basically, the SQL data adapter object is allowing us to populate the tables in the dataset. We can also build and fill each table data in the dataset by using data source and data adapter.
In the below example, we are finding the number of tables from the dataset object.
Below ASP.NET code is used to find the number of tables from the dataset object as follows:
</script> -- End of script tags
<html xmlns="http://www.myexample.com/xhtml"> -- Start of html tag
<head id="Head1" runat="server"> -- Start of head tag
<title>Tables in Dataset</title> -- Start and end of title tag
</head> -- End of head tags
<body> -- Start of body tags
<form id="Dataset Table" runat="server"> -- Start of form id tag
<div> -- Start of div tags.
<asp:Button ID="Button1" runat="server" Text="Dataset" onclick="Button1_Click" />
<br /> -- End of br tag.
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<br /> -- End of br tag.
<asp:Label ID="Label1" runat="server" Text="Table"></asp:Label>
</div> -- End of div tags
</form> -- End of form tags
</body> -- End of body tags
</html> -- End of html tags
Example of Dataset in ASP.NET
Given below is the example mentioned:
1. Create a new project.
Name – Dataset
2. Drag the data grid view.
3. Extract the data.
In the below example, we are checking the count of the product table. We can see that the product table count is 65.
private void btnGetData_Click(object sender, EventArgs e)
string datacon = @"Data Source=.\SQLEXPRESS;Initial Catalog=ComputerShop;Integrated Security=True";
string Query = "SELECT count(*) FROM products";
SqlDataAdapter datasetadapter = new SqlDataAdapter (Query, datacon);
DataSet dataset = new DataSet ();
adapter.Fill (dataset, "products");
dataGridView1.DataSource = set.Tables ["products"]; }
Conclusion – Dataset in ASP.NET
The dataset in ASP.NET can be filled dynamically or by using a data source. Dataset in ASP.NET contains columns, primary key, rows, data table object relations and constraints. Dataset in ASP.NET is providing representation of disconnected results from the data sources. Dataset is very important in ASP.NET.
We hope that this EDUCBA information on “Dataset in ASP.NET” was beneficial to you. You can view EDUCBA’s recommended articles for more information.