locked
Passing SqlDataReader through a function RRS feed

  • Question

  • User-1689344329 posted

    I have a function in my class which it is return a SqlDataReader object. But when I want read from the SqlDataReader below error arisen.

    How can I Open this SqlDataReader again and fetch data from it?


    Invalid attempt to call Read when reader is closed.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: Invalid attempt to call Read when reader is closed.

    Source Error: 

    Line 29:             SqlDataReader sdr =  APIcls.SelectUser ();
    Line 30: 
    Line 31: while ( sdr.Read () ) 
    Line 32: Response.Write ( (string)sdr["User_Name"] );
    Line 33:


    Source File: E:\ContactAPI\Test1.aspx.cs    Line: 31 


    Thank you very much.

    Friday, February 24, 2012 12:18 PM

Answers

  • User-1590642642 posted

    The function that returns the Reader is probably closing it.   If you have created the reader with a using(){ } statement, then that is what is happening.   Check that function and remove any code that might be closing or disposing the reader.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 24, 2012 12:25 PM

All replies

  • User-1590642642 posted

    The function that returns the Reader is probably closing it.   If you have created the reader with a using(){ } statement, then that is what is happening.   Check that function and remove any code that might be closing or disposing the reader.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 24, 2012 12:25 PM
  • User-1689344329 posted

    Thank you very much dude. That's work. thx again and good luck

    Saturday, February 25, 2012 7:58 AM