Arvin Quizon
Arvin Quizon

Reputation: 23

"There is already an open DataReader associated with this Command which must be closed first"

My code here returns the error "There is already an open DataReader associated with this Command which must be closed first." and I already added the code "MultipleActiveResultSets=true" on my web.config

        ReportViewer1.Visible = true;
        ReportViewer1.ProcessingMode = ProcessingMode.Local;

        var sqlConnection = new SQLConnectionStrings();
        var conReport = new SqlConnection(sqlConnection.strConn);
        var cmdReport = new SqlCommand();
        var myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = cmdReport;

        SqlDataReader drReport;

        DataSet dsReport = new DataSet();
        //PerfLogDataSet();


        cmdReport.CommandType = CommandType.Text;
        cmdReport.Connection = conReport;
        conReport.Open();
        cmdReport.CommandText = baseQuery;
        drReport = cmdReport.ExecuteReader();
        myDataAdapter.Fill(dsReport); //where the error occurs
        dsReport.Tables[0].Load(drReport);
        drReport.Close();
        conReport.Close();

Upvotes: 0

Views: 870

Answers (2)

Farrokh
Farrokh

Reputation: 1167

try this code if you want to fill dataset...

    ReportViewer1.Visible = true;
    ReportViewer1.ProcessingMode = ProcessingMode.Local;

    var sqlConnection = new SQLConnectionStrings();
    var conReport = new SqlConnection(sqlConnection.strConn);
    var cmdReport = new SqlCommand(baseQuery);
    var myDataAdapter = new SqlDataAdapter(cmdReport,sqlConnection );

    DataSet dsReport = new DataSet();

    conReport.Open();
    myDataAdapter.Fill(dsReport);
    conReport.Close();

but if you want anything else ,describe it ,more ;)

Upvotes: 0

Paul Abbott
Paul Abbott

Reputation: 7211

You're attempting to simultaneously use a SqlDataAdapter and SqlDataReader with the same SqlCommand.

SqlDataAdapter will load the result set into a DataSet or DataTable. SqlDataReader is for looping over the result set one record at a time, recordset-style. They are two different methods for accessing data with two different intents, and it makes no sense to use both.

If you're trying to load dsReport, get rid of

SqlDataReader drReport;
drReport = cmdReport.ExecuteReader();
dsReport.Tables[0].Load(drReport);
drReport.Close();

As you are already loading it with the adapter using

myDataAdapter.Fill(dsReport);

Upvotes: 1

Related Questions