Click here to Skip to main content
15,916,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Please help
I want Bind tow DropDownLists control in the RowDataBound(object sender) of the GridView
But I face the below error in this line da.Fill(dt); DropDegree

Error:
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
Additional information: There is already an open DataReader associated with this Command which must be closed first.
How I do close the DataReader?

thanks

My Example Code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
        {
            DropDownList DropDegree = (DropDownList)e.Row.FindControl("DropDownListDegree");
            DropDownList DropSubField = (DropDownList)e.Row.FindControl("DropDownListField");
           
                DataTable dt = new DataTable();
                string sql = "select Field_ID,Field_name_english FROM Field_TB";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(dt);
                DropDegree.DataSource = dt;
                DropDegree.DataTextField = "Field_name_english";
                DropDegree.DataValueField = "Field_ID";
                DropDegree.DataBind();
                DropDegree.SelectedValue = "your degree";
              
                //Adding "Please select country" option in dropdownlist
                DropDegree.Items.Insert(0, new ListItem("Select your degree", "0"));
                //Adding initially value to "SubField" 
                DropSubField.Items.Insert(0, new ListItem("Select your field", "0"));

            //--------------------------------------------------------
                
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.CommandText = "select SubField_ID,SubField_Name_english FROM SubField_TB WHERE Field_ID=@FieldID";
                sqlCmd.Parameters.AddWithValue("@FieldID", DropDegree.SelectedValue);
                sqlCmd.Connection = con;
                con.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(sqlCmd);
                DataTable dt1 = new DataTable();
                da1.Fill(dt1);
                DropSubField.DataSource = dt;
                DropSubField.DataValueField = "SubField_ID";
                DropSubField.DataTextField = "SubField_Name_english";
                DropSubField.DataBind();
                con.Close();
                //Adding "Please select SubField" option in dropdownlist
                DropSubField.Items.Insert(0, new ListItem("Select your field", "0"));
            
           
        }// End of IF
    }//
Posted

1 solution

you forget to open the Connection while executing the first Query

C#
DataTable dt = new DataTable();
               string sql = "select Field_ID,Field_name_english FROM Field_TB";
if (con.State == ConnectionState.Closed)
           {
               con.Open();
           }
               SqlDataAdapter da = new SqlDataAdapter(sql, con);
               da.Fill(dt);
               DropDegree.DataSource = dt;
               DropDegree.DataTextField = "Field_name_english";
               DropDegree.DataValueField = "Field_ID";
               DropDegree.DataBind();
               DropDegree.SelectedValue = "your degree";
if (con.State == ConnectionState.Open)
           {
               con.Close();
           }
 
Share this answer
 
Comments
Member 11240896 3-Mar-15 19:34pm    
thanks dear
King Fisher 3-Mar-15 23:11pm    
Welcome dr ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900