Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Error while using NextResult fuction with datareader

cannot get second table result and error on second NextResult line
"
invalid attempt to call nextresult when reader is closed
"



C#
using (SqlConnection myCon = DBCon)
        {
            try
            {
  string Qry =  @"SELECT [OPSProcedure],[OPSInsertedOn],[OPSInsertedBy]
	    FROM [Operation] where OPSID =  '" + opId + "';";
        Qry += @"SELECT  LKCPID  FROM dbo.ConcurrentProcedure  where CPOperationID = '" + opId + "';";
        Qry += @"SELECT  IOperaitonID  FROM  dbo.LkupIntraOperativeAdverseEvents   where IOperaitonID = '" + opId + "';";
   
                myCon.Open();
                SqlCommand myCommand = new SqlCommand(Qry, myCon);
                myCommand.CommandType = CommandType.Text;
                SqlDataReader sqlReader = myCommand.ExecuteReader();
                DataSet dr = new DataSet();
                if (sqlReader.HasRows)
                {
                    dt1.Load(sqlReader);
                    if(sqlReader.NextResult())
                    {
                    dt2.Load(sqlReader);
                    }
                    if (sqlReader.NextResult())
                    {
                    dt3.Load(sqlReader);
                    }
                    
                }
                sqlReader.Close();
           }
            catch (Exception ex)
            {
               
            }

        }


What I have tried:

i have tried using below code for multiple result
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900