Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "There is already an open DataReader associated with this Command which must be closed first" when using 2 distinct commands

Tags:

c#

I have this legacy code :

 private void conecta()  {        if (conexao.State == ConnectionState.Closed)          conexao.Open();  }   public List<string[]> get_dados_historico_verificacao_email_WEB(string email)  {      List<string[]> historicos = new List<string[]>();      conecta();       sql =           @"SELECT *           FROM historico_verificacao_email           WHERE nm_email = '" + email + @"'           ORDER BY dt_verificacao_email DESC, hr_verificacao_email DESC";       com = new SqlCommand(sql, conexao);      SqlDataReader dr = com.ExecuteReader();       if (dr.HasRows)      {          while (dr.Read())          {              string[] dados_historico = new string[6];              dados_historico[0] = dr["nm_email"].ToString();              dados_historico[1] = dr["dt_verificacao_email"].ToString();              dados_historico[1] = dados_historico[1].Substring(0, 10);              dados_historico[2] = dr["hr_verificacao_email"].ToString();              dados_historico[3] = dr["ds_tipo_verificacao"].ToString();               sql =                   @"SELECT COUNT(e.cd_historico_verificacao_email) QT                   FROM emails_lidos e                   WHERE e.cd_historico_verificacao_email =                       '" + dr["cd_historico_verificacao_email"].ToString() + "'";               tipo_sql = "seleção";              conecta();              com2 = new SqlCommand(sql, conexao);               SqlDataReader dr3 = com2.ExecuteReader();              while (dr3.Read())              {                  //quantidade de emails lidos naquela verificação                  dados_historico[4] = dr3["QT"].ToString();               }              dr3.Close();              conexao.Close();               //login              dados_historico[5] = dr["cd_login_usuario"].ToString();              historicos.Add(dados_historico);          }          dr.Close();      }      else      {           dr.Close();      }       conexao.Close();      return historicos;  } 


I have created two separates commands to correct the issue, but it still continues: "There is already an open DataReader associated with this Command which must be closed first".

An additional info: the same code is working in another app.

like image 761
alejandro carnero Avatar asked Aug 27 '13 20:08

alejandro carnero


People also ask

How do you fix there is already an open DataReader associated with this Command which must be closed first?

This can be easily solved by allowing MARS in your connection string. Add MultipleActiveResultSets=true to the provider part of your connection string (where Data Source, Initial Catalog, etc.

How do I close SQL data reader?

The Close method may either be called directly or through the Dispose method, disposing directly or in the context of the using statement block. The Close method populates the values for output parameters, return values and RecordsAffected on the SqlDataReader by consuming any pending results.


1 Answers

Just add the following in your connection string:

MultipleActiveResultSets=True; 
like image 50
Ankit Avatar answered Sep 23 '22 06:09

Ankit