【问题标题】:There is already an open datareader error when filling Gridviews填充 Gridview 时已经存在打开数据读取器错误
【发布时间】:2017-04-08 23:56:43
【问题描述】:

我已经查看了几篇关于尝试解决此问题的帖子,但都没有奏效。 我已经在使用多个活动结果。 我确保关闭阅读器连接。 我正在使用不同的连接。 我为阅读器、数据表、阅读器、命令使用唯一的名称...... 我被卡住了。

错误是:已经有一个打开的 DataReader 与此命令关联,必须先关闭。

我已将错误行标记为“*****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;
    }


}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案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;
                    }
                }
            }
        }
    

    【讨论】:

    • 哇,这就是问题所在。我实际上最终消除了适配器。我只发布了该函数中大约 1/3 的代码,我需要读者对这些列进行一些额外的操作。
    【解决方案2】:

    cmd33.ExecuteReader 只能调用一次。您正在为每一行执行它。

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多