none
There is already an open DataReader associated with this Command which must be closed first. RRS feed

  • Question

  • Hello,

    Using a Windows service to read/update a dataset. There are mutiple steps and each step uses the same Connection object. The service runs about every hour and calls the block below for every record in the queue to process. When there are many rows, say over 2K, I am getting the error: There is already an open DataReader associated with this Command which must be closed first. If the queue is low, <2, the code executes fine.

    Any help?

    My Code looks like:

    try
       {
     //Define SqlCommands and SqlDataAdapters
    SqlCommand cmWeb = new SqlCommand("xxx", cnXXX);
    cmWeb.CommandTimeout = 0;
    SqlDataAdapter daRunProc = new SqlDataAdapter(cmWeb);
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("Output");
    dt.BeginLoadData();
    daRunProc.Fill(dt);
    dt.EndLoadData();
    
    
    dt.Dispose();
    ds.Dispose();
    daRunProc.Dispose();
    }
    catch (Exception ex)
    {
    common.LogError(
    }


    Friday, May 4, 2012 1:47 PM

Answers

  • Start with removing all your dispose methods.

    The dataAdapter has overridden in its close (which is automatically called if the open is done by the dataadapter and not done before) the dispose so you are probably only mixing something up.

    The Dispose does in the AdoNet situation nothing more than is already done build in done by other methods.

    (the dataset, and the datatable don't even have unmanaged resources so the dispose method is only available because it is inherited from components) 


     


    Success
    Cor


    • Proposed as answer by SKracker Wednesday, May 9, 2012 8:49 AM
    • Marked as answer by Allen_MSDNModerator Saturday, May 19, 2012 6:45 AM
    • Edited by Cor Ligthert Saturday, May 19, 2012 10:51 AM overloaded = overriden
    Monday, May 7, 2012 7:43 AM
  • Do not maintain persistent or shared Connection objects. Once a Connection has been used it should be closed so that the connection can be released to the connection pool. Create and open a new Connection object for subsequent data access.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, May 9, 2012 12:11 PM

All replies

  • Start with removing all your dispose methods.

    The dataAdapter has overridden in its close (which is automatically called if the open is done by the dataadapter and not done before) the dispose so you are probably only mixing something up.

    The Dispose does in the AdoNet situation nothing more than is already done build in done by other methods.

    (the dataset, and the datatable don't even have unmanaged resources so the dispose method is only available because it is inherited from components) 


     


    Success
    Cor


    • Proposed as answer by SKracker Wednesday, May 9, 2012 8:49 AM
    • Marked as answer by Allen_MSDNModerator Saturday, May 19, 2012 6:45 AM
    • Edited by Cor Ligthert Saturday, May 19, 2012 10:51 AM overloaded = overriden
    Monday, May 7, 2012 7:43 AM
  • Do not maintain persistent or shared Connection objects. Once a Connection has been used it should be closed so that the connection can be released to the connection pool. Create and open a new Connection object for subsequent data access.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, May 9, 2012 12:11 PM
  • Hi Val Mazur,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 2:49 AM
    Moderator