none
How does NextResult method works in SqlDataReader?

    Question

  • I am using NextResult method in one of my pages and it's not displaying exactly what I'm looking for. If I have a result batch of, let's say, 3 Select statement, the NextResult method means any result after the first result, correct? But i am getting only two query result instaed of 3rd. Please correct the code and tell the reason.

    Thanks.

    Note : I am learner programmer.

          using (SqlConnection con = new SqlConnection(cs))
                 {
                    SqlCommand cmd = new SqlCommand("select * from name; select * from tblinfo ; select productid,ProductName,unitPrice from tblproduct", con);
                    con.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {

                        NameGridView.DataSource = rdr;
                        NameGridView.DataBind();

                        while (rdr.NextResult())
                        {
                            InfoGridView.DataSource = rdr;
                            InfoGridView.DataBind();
                        }

                        while (rdr.NextResult())
                        {
                            ProductGridView.DataSource = rdr;
                            ProductGridView.DataBind();
                        }

                    }
                }

                        
    Tuesday, March 21, 2017 11:57 AM

Answers

  • Try this:

    NameGridView.DataSource = rdr;
    NameGridView.DataBind();
    
    if( rdr.NextResult())
    {
       InfoGridView.DataSource = rdr;
       InfoGridView.DataBind();
    
       if( rdr.NextResult())
       {
          ProductGridView.DataSource = rdr;
          ProductGridView.DataBind();
       }
    }

    • Marked as answer by libra_ali786 Tuesday, March 21, 2017 2:06 PM
    Tuesday, March 21, 2017 1:20 PM