none
Invalid attempt to call Read when reader is closed RRS feed

  • Question

  • Hi,

    I am getting "Invalid attempt to call Read when reader is closed" error in one of my pages.I have used the following method in my class file,

     

    public  
      
    class ExecuteQuery  
    {  
      
    OleDbConnection conn;  
    OleDbCommand cmd;  
    DataTable dt;  
    OleDbDataAdapter da;  
    OleDbDataReader dr;  
       
      
    public ExecuteQuery()  
    {  
      
    conn =  
      
    new OleDbConnection();  
    conn.ConnectionString =  
      
    DBConnection.GetConnectionString();  
    }  
      
    public  
      
    OleDbDataReader ExecuteReader(string ReaderQuery)  
    {  
      
    cmd =  
      
    new OleDbCommand(ReaderQuery, conn);  
    conn.Open();  
      
    dr = cmd.ExecuteReader(  
      
    CommandBehavior.CloseConnection);  
    conn.Close();  
      
    return dr;  
    }  
      
    }  
       
    In the aspx.cs file I have used the following code,  
       
    private void ddlfill()  
    {  
      
    try  
    {  
      
    ddlquestion.Items.Add(  
      
    "");  
    ddlquestion.SelectedIndex = 0;  
      
    string query = "select Hint_Question from Tbl_Hintquestion order by Hint_No ASC";  
    bool abc = EQuery.ExecuteReader(query).Read();  
    while (abc == true)  
    {  
      
    ddlquestion.Items.Add(dr[  
      
    "Hint_Question"].ToString());  
    abc = EQuery.ExecuteReader(query).Read();  
      
    }  
      
    }  
      
    catch (Exception EXE)  
    {  
      
    Alert.Show(EXE.Message);  
    }  
      
    }  
    can some one help me out in fixing this?

    Sunday, January 3, 2010 6:25 PM

Answers

  • conn.Close();

    Remove this from the function executereader. The calling function must close the reader which in turn would close the connection.

    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Proposed as answer by Haripraghash Monday, January 4, 2010 6:39 AM
    • Marked as answer by VMazurModerator Monday, January 4, 2010 11:00 AM
    Monday, January 4, 2010 6:37 AM