Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

SqlDataReader object NextResult() method - Part 9

Suggested Videos
Part 6 - Sql injection prevention
Part 7 - Calling a stored procedure with output parameters
Part 8 - SqlDataReader object in ado.net

In this video we will learn about retrieving two or more result sets using the SqlDataReader object's NextResult() method. The follwoing SqlCommand object returns two result-sets, one from - tblProductInventory and the other from tblProductCategories.
SqlCommand command = new SqlCommand("select * from tblProductInventory; select * from tblProductCategories", connection);

When you run this code only the result set from tblProductInventory is displayed. The result-set from tblProductCategories is not shown.
string ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
    connection.Open();
    SqlCommand command = new SqlCommand("select * from tblProductInventory; select * from tblProductCategories", connection);
    using (SqlDataReader reader = command.ExecuteReader())
    {
        ProductsGridView.DataSource = reader;
        ProductsGridView.DataBind();

        CategoriesGridView.DataSource = reader;
        CategoriesGridView.DataBind();
    }
}



To retrieve the second result-set from SqlDataReader object, use the NextResult() as shown in the code snippet below. The NextResult() method returns true and advances to the next result-set. 
string ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
    connection.Open();
    SqlCommand command = new SqlCommand("select * from tblProductInventory; select * from tblProductCategories", connection);
    using (SqlDataReader reader = command.ExecuteReader())
    {
        ProductsGridView.DataSource = reader;
        ProductsGridView.DataBind();

        while (reader.NextResult())
        {
            CategoriesGridView.DataSource = reader;
            CategoriesGridView.DataBind();
        }
    }
}

The SqlDataReader object's Read() method is used to loop thru the rows in a given result set, where as the NextResult() method is used to loop thru multiple  result sets.



Sql script to create and populate the required tables we used in this demo.
Create table tblProductInventory 
(
ProductId int identity primary key,
ProductName nvarchar(50),
UnitPrice int
)

Insert into tblProductInventory values('iPhone',350)
Insert into tblProductInventory values('Apple Laptops',1250)
Insert into tblProductInventory values('Books',110)
Insert into tblProductInventory values('Acer Laptops',1150)
Insert into tblProductInventory values('iPads',450)

Create table tblProductCategories
(
CategoryId int identity primary key,
CategoryName nvarchar(50),
)

Insert into tblProductCategories values ('Electronics')
Insert into tblProductCategories values ('Sports')
Insert into tblProductCategories values ('Kitchen')

5 comments:

  1. Hi Sir
    I tried this code for more than two gridviews but in output showing only two Tables .
    Please help me sir
    my code written below
    using (SqlDataReader Reader = SqlQuerry1.ExecuteReader())
    {
    GridView1.DataSource = Reader;
    GridView1.DataBind();

    while (Reader.NextResult())
    {


    GridView2.DataSource = Reader;
    GridView2.DataBind();
    }
    while (Reader.NextResult())
    {
    GridView3.DataSource = Reader;
    GridView3.DataBind();
    }

    ReplyDelete
    Replies
    1. The following code for using more than two GridViews controls in the web page by using DataReader NextResult() method .

      using (SqlDataReader Reader = SqlQuerry1.ExecuteReader())
      {
      GridView1.DataSource = Reader;
      GridView1.DataBind();

      while (Reader.NextResult())
      {


      GridView2.DataSource = Reader;
      GridView2.DataBind();
      while (Reader.NextResult())
      {
      GridView3.DataSource = Reader;
      GridView3.DataBind();
      while (Reader.NextResult())
      {
      GridView4.DataSource = Reader;
      GridView4.DataBind();
      }
      }
      }

      Delete
  2. using (SqlConnection connection = new SqlConnection(ConnectionString))
    {
    connection.Open();
    SqlCommand command = new SqlCommand("select * from tblProductInventory; select * from tblProductCategories;select * from tblEmployees", connection);
    using (SqlDataReader reader = command.ExecuteReader())
    {
    GridView1.DataSource = reader;
    GridView1.DataBind();

    reader.NextResult();
    GridView2.DataSource = reader;
    GridView2.DataBind();

    reader.NextResult();
    GridView3.DataSource = reader;
    GridView3.DataBind();


    }

    }

    ReplyDelete
  3. Good Work...Its Working For me...

    ReplyDelete
  4. You just always forget to open the connection #sir :P

    ReplyDelete

It would be great if you can help share these free resources