2017-04-08 34 views
0

我已經看過幾篇文章,試圖解決這個問題,但他們都沒有工作。 我已經在使用多個活動結果。 我正在確保關閉閱讀器連接。 我正在使用不同的連接。 我爲閱讀器,數據表,閱讀器,命令使用唯一名稱... 我被卡住了。填寫Gridviews時已經有一個open datareader錯誤

錯誤是:已經有一個打開的DataReader與這個Command關聯,必須先關閉它。

我用「***** error here *****」標記了錯誤行。

代碼:

protected void gridviewsched_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    string nametime; 
    string name; 
    string time; 
    string initid; 
    string timeinitid = null; 
    GridView gridviewschedsub = (GridView)e.Row.FindControl("gridviewschedsub"); 
    GridView gridviewschedcplt = (GridView)e.Row.FindControl("gridviewschedcplt"); 

    using (SqlConnection con = new SqlConnection("Data Source=localhost\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Public\\public website\\slDataBase.mdf;Integrated Security=True;Trusted_Connection=True; MultipleActiveResultSets=True;")) 
    { 
     con.Open(); 
     DataTable dz = new DataTable(); 
     dz.Columns.Add("age"); 
     dz.Columns.Add("sex"); 
     dz.Columns.Add("address"); 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      string id = gridviewsched.DataKeys[e.Row.RowIndex].Value.ToString(); 
      using (var cmd = new SqlCommand("SELECT age,sex,address FROM precordTable WHERE Id='" + id + "'", con)) 
      { 
       using (SqlDataReader reader = cmd.ExecuteReader()) 
       { 
        //List<string> namedatelist = new List<string>(); 
        while (reader.Read()) 
        { 
         DataRow dr = dz.NewRow(); 
         dr["age"] = reader[0].ToString(); 
         dr["sex"] = reader[1].ToString(); 
         dr["address"] = reader[2].ToString(); 
         dz.Rows.Add(dr); 
        } 
        reader.Close(); 
       } 
       gridviewschedsub.DataSource = dz; 
       gridviewschedsub.DataBind(); 
       con.Close(); 
      } 
      using (var cmd3 = new SqlCommand("SELECT name, initid FROM precordTable WHERE Id='" + id + "'", con)) 
      { 
       con.Open(); 
       using (SqlDataReader reader = cmd3.ExecuteReader()) 
       { 

        List<string> namedatelist = new List<string>(); 
        while (reader.Read()) 
        { 
         name = reader["name"].ToString(); 
         initid = reader["initid"].ToString(); 
         time = DateTime.Now.ToString("MM-dd-yyyy"); 
         time = Regex.Replace(time, "[^0-9a-zA-Z]+", ""); 
         namedatelist.Add(name + time); 
         timeinitid = time + "$" + initid; 

        } 
        Session["timeinitid"] = timeinitid; 
        nametime = Regex.Replace(namedatelist[0].ToString(), "[^0-9a-zA-Z]+", ""); 
        reader.Close(); 
       } 

      } 
      var cmd2 = new SqlCommand("select case when exists((select * from [C:\\USERS\\PUBLIC\\PUBLIC WEBSITE\\SLDATABASE.MDF].INFORMATION_SCHEMA.tables where table_name = 'D" + timeinitid + "ou')) then 1 else 0 end", con); 

      if ((int)cmd2.ExecuteScalar() == 1) 
      { 

       string fQuery = "select item, scheduled from D" + timeinitid + "ou where 0 = 1"; 
       string pQuery = "select item, scheduled from D" + timeinitid + "ou where initialed = '' and prescdr IS NULL and item != '';"; 
       SqlDataAdapter sdyn = new SqlDataAdapter(); 
       DataTable cpltTable = new DataTable(); 

       cpltTable = GetData(pQuery); 

       gridviewschedcplt.DataSource = cpltTable; 
       gridviewschedcplt.DataBind(); 
       con.Close(); 
      } 
      else 
      { 

       return; 
      } 



     } 
    } 
    for (int j = 0; j < gridviewsched.Rows.Count; j++) 
    { 
     for (int i = 3; i < 9; i++) 
     { 
      gridviewsched.Rows[j].Cells[i].RowSpan = 2; 
     } 
     gridviewsched.Rows[j].Cells[2].RowSpan = 2; 
    } 
} 

private static DataTable GetData(string pQuery) 
{ 
    string schedtime; 
    string nowtime; 
    SqlDataAdapter sd1 = new SqlDataAdapter(); 
    DataTable dTable = new DataTable(); 
    using (SqlConnection conn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Public\\public website\\slDataBase.mdf;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True;")) 
    { 
     conn.Open(); 
     SqlCommand cmd33 = new SqlCommand(pQuery, conn); 
     using (SqlDataReader reader99 = cmd33.ExecuteReader()) 
     { 
      while (reader99.Read()) 
      { 
       sd1.SelectCommand = cmd33; 
       ***error here**** sd1.Fill(dTable); 
       DataRow newcpltTablerow = dTable.NewRow(); 
       newcpltTablerow["item"] = reader99["item"].ToString(); 
       dTable.Rows.Add(newcpltTablerow); 

      } 

      reader99.Close(); 
     } 

     return dTable; 
    } 


} 

回答

1

,如果你使用的是SqldataAdapter你不需要SqlDataReader。由於您在已經打開的適配器中打開了一個閱讀器,您正在收到該錯誤。做到這一點,而不是(編譯沒有那麼根據需要調整和更改查詢到你):

using (SqlConnection con = new SqlConnection(constring)) 
    { 
     using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con)) 
     { 
      cmd.CommandType = CommandType.Text; 
      using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) 
      { 
       using (DataTable dt = new DataTable()) 
       { 
        sda.Fill(dt); 
        dataGridView1.DataSource = dt; 
       } 
      } 
     } 
    } 
+0

哇,這是問題。我實際上最終取消了適配器。我只發佈了該功能中大約1/3的代碼,我需要讀者對列進行一些額外的操作。 – anesthetic

0

cmd33.ExecuteReader只能被調用一次。您正在爲每一行執行它。

相關問題