无效操作异常未被用户cod处理 [英] Invalid Operation Exception was unhandled by user cod
问题描述
你好请帮助
我想在GridView的RowDataBound(对象发送者)中绑定两个DropDownLists控件
但是我在这行中遇到以下错误da.Fill (DT); DropDegree
错误:
System.Data.dll中出现System.InvalidOperationException类型的异常,但未在用户代码中处理
附加信息:已经有一个与此命令关联的开放DataReader必须先关闭。
如何关闭DataReader?
谢谢
我的示例代码
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
}//
推荐答案
你忘记在执行第一个查询时打开连接
you forget to open the Connection while executing the first Query
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();
}
这篇关于无效操作异常未被用户cod处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!