填充Gridviews时已存在打开的datareader错误

时间:2017-04-08 23:56:44

标签: c# asp.net gridview

我已经查看了几个已经尝试修复此问题的帖子,但没有一个正在运行。 我已经使用了多个有效结果。 我确保关闭阅读器连接。 我正在使用不同的连接。 我正在为读者,数据表,读者,命令使用唯一的名称...... 我被卡住了。

错误是:已经有一个与此命令关联的打开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;
    }


}

2 个答案:

答案 0 :(得分:1)

如果您使用SqlDataReader,则不需要SqldataAdapter。您收到错误是因为您在已打开的适配器中打开了一个阅读器。改为执行此操作(未编译,根据需要进行调整并将查询更改为您的查询):

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 :(得分:0)

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