Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. SqlDataReader fails with multiple resultsets

SqlDataReader fails with multiple resultsets

Scheduled Pinned Locked Moved .NET (Core and Framework)
database
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Husein
    wrote on last edited by
    #1

    Hi, I have a multiple resultset returned by the stored procedure (4 select statements returning different values). When I call SqlCommand's ExecuteReader on the following piece of code: cn = new System.Data.SqlClient.SqlConnection(ConnectionString); cn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "spResSupervisorSwitchboard"; cmd.CommandType = CommandType.StoredProcedure; SqlDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { lblWaitingFirstEntry.Text = rdr[0].ToString(); // System.InvalidOperationException } rdr.NextResult(); The connection is opened fine, the stored procedure name is correct, stored procedure returns values when executed in SQL Query Analyzer... but it doesn't work in my code. :mad: Cheers

    A 1 Reply Last reply
    0
    • H Husein

      Hi, I have a multiple resultset returned by the stored procedure (4 select statements returning different values). When I call SqlCommand's ExecuteReader on the following piece of code: cn = new System.Data.SqlClient.SqlConnection(ConnectionString); cn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "spResSupervisorSwitchboard"; cmd.CommandType = CommandType.StoredProcedure; SqlDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { lblWaitingFirstEntry.Text = rdr[0].ToString(); // System.InvalidOperationException } rdr.NextResult(); The connection is opened fine, the stored procedure name is correct, stored procedure returns values when executed in SQL Query Analyzer... but it doesn't work in my code. :mad: Cheers

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      from what i see, code is not wrapped in a try catch block. you also don't close the connection, and rdr.NextResult is not needed since rdr.Read() enumerates through set in a DataReader. plus ur opening the connection before ur setting cmd fields. try this cn = new System.Data.SqlClient.SqlConnection(ConnectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "spResSupervisorSwitchboard"; cmd.CommandType = CommandType.StoredProcedure; try{ SqlDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { lblWaitingFirstEntry.Text = rdr[0].ToString(); } }catch(SqlException ex) { lblWaitingFirstEntry.Text = ex.ToString(); } finally { con.close(); }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups