In this article, we shall learn how to work with multiple results set using DataSet.
ADO.NET is a mechanism to communicate with the database. It allows us to work in connected (database connection is alive while the operation is performed) as well as disconnected architecture (database connection is closed and operation can be performed on the data).
Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.
In order to retrieve more than one result sets from the Stored Procedure or multiple select statements at one go
using DataSet, we can use this approach.
Below is the GridView code on my ASPX Page
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" />
<asp:GridView ID="GridView2" runat="server" EnableViewState="false" />
In the above code snippet, we have two GridViews.
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
DataSet dSet = new DataSet();
// get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
// write the sql statement to execute
string sql = "SELECT AutoID, FirstName, LastName FROM PersonalDetail ORDER BY FirstName; " +
"SELECT AutoId, FileName FROM Files Order By FileName ASC";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
// 1st result set
GridView1.DataSource = dSet.Tables;
// 2nd result set
GridView2.DataSource = dSet.Tables;
In the code behind, we have used
DataSet to execute a SQL statement that has two SELECT statements separated by semi-column (
;) (Multiple select statements returns multiple result sets from the database).
DataSet is a collection of DataTables that can hold multiple result sets, so we have used DataSet here. Instead of the SQL statement, we can also use a stored procedure that can return multiple result set by executing multiple select statements.
All the select statements executed will have a separate
DataTable created into the DataSet and we can
access them by using the indexer of the Tables collection (like we have used dSet.Tables and
dSet.Tables to return the 1st and the 2nd result set).
My output looks like above picture.
Thanks for reading, hope you are liking my series of ADO.NET articles that describes several frequently used solutions of ADO.NET problems. To watch video tutorials of ADO.NET and ASP.NET problem solutions, click here.