I am getting this error when the gridview is created with an empty dataset. What I am trying to do is populate a dropdown list in the EmptyDataTemplate. From reading other posts the error is caused by use SqlDataReader bind gridview after SqlConnection obejct closed. However, doesn't row created fire after the gridview row is populated?
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.EmptyDataRow)
{
string connectionString3 = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
DropDownList ddl = (DropDownList)e.Row.FindControl("EOCEmpty");
using (SqlConnection conn3 = new SqlConnection(connectionString3))
{
SqlCommand cmd3 = new SqlCommand("SELECT DISTINCT GLAccountEOC, EOCDescription FROM Acct_GLAccount WHERE CostCenter = @CostCenter Order By EOCDescription", conn3);
cmd3.Parameters.Add("@CostCenter", System.Data.SqlDbType.Int);
cmd3.Parameters["@CostCenter"].Value = "3215";
try
{
conn3.Open();
SqlDataReader cmdreader3 = cmd3.ExecuteReader();
ddl.DataSource = cmdreader3;
ddl.DataValueField = "GLAccountEOC";
ddl.DataTextField = "EOCDescription";
ddl.DataBind();
cmdreader3.Close();
}
finally
{
conn3.Close();
}
}
}
}