Search code examples
c#datareader

There is already an open DataReader. There is different comaand use but not resolve


public void createGenericControl()
{
    string selectSQL = @"SELECT id,[empId],[eventId],[eventname]
    FROM [getTask] where empid=111 ";
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    con.Open();

    SqlDataReader rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {

      HtmlGenericControl tabs = new HtmlGenericControl("div");

      tabs.Attributes.Add("class", "panel panel-default");
      HtmlGenericControl panel = new HtmlGenericControl("div");
      panel.Attributes.Add("class", "panel-heading");


      HtmlGenericControl headAnchor = new HtmlGenericControl("a");
      headAnchor.InnerText = rdr["eventName"].ToString();
      headAnchor.Attributes.Add("class", "panel-title");
      headAnchor.Attributes.Add("data-toggle", "collapse");
      headAnchor.Attributes.Add("data-parent", "panel-530164");
      headAnchor.Attributes.Add("href", "#panel-element-" + rdr["id"].ToString());

      panel.Controls.Add(headAnchor);
      tabs.Controls.Add(panel);

      HtmlGenericControl panelElement = new HtmlGenericControl("div");

      panelElement.Attributes.Add("class", "panel-collapse collapse in");
      panelElement.Attributes.Add("id", "panel-element-" + rdr["id"].ToString());
      HtmlGenericControl panelbody = new HtmlGenericControl("div");

      panelbody.Attributes.Add("class", "panel-body");
      HtmlGenericControl row = new HtmlGenericControl("div");

      row.Attributes.Add("class", "row");
      HtmlGenericControl col = new HtmlGenericControl("div");

      col.Attributes.Add("class", "col-md-15");


      HtmlGenericControl ul = new HtmlGenericControl("ul");

      HtmlGenericControl li = new HtmlGenericControl("li");
      string selectSQL1 = @"select myevents.eventName,eventDescription from  myevents inner join
    dbo.getTask on myevents.eventName=gettask.eventName ";
      SqlCommand locationCommand = new SqlCommand(selectSQL1, con);
      SqlDataAdapter adapter1 = new SqlDataAdapter(locationCommand);
      SqlDataReader rdr1 = locationCommand.ExecuteReader();
      while (rdr1.Read())
      {
          li = new HtmlGenericControl("li");



          li.InnerText = rdr1[1].ToString();
          ul.Controls.Add(li);
          col.Controls.Add(ul);

          row.Controls.Add(col);

          panelbody.Controls.Add(row);
          panelElement.Controls.Add(panelbody);
          panelElement.Controls.Add(panelbody);
          panelElement.Controls.Add(panelbody);
          tabs.Controls.Add(panelElement);
          ulTabs.Controls.Add(tabs);
          StringWriter sw = new StringWriter();
          HtmlTextWriter w = new HtmlTextWriter(sw);
          ulTabs.RenderControl(w);
          string s = sw.GetStringBuilder().ToString();

      }
      con.Close();
    }
}

Solution

  • MARS is what you are looking for. You have start new DataReader before finishing the previous one. It is not allowed by default, so you have to specify that option in the connection string when you create the connection:

    string connectionString = "Data Source=MSSQL1;" +   
    "Initial Catalog=AdventureWorks;Integrated Security=SSPI;" +  
    "MultipleActiveResultSets=True";