阅读器关闭时尝试调用Read无效
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace DepartMent
{
public partial class _Default : System.Web.UI.Page
{
// protected System.Web.UI.WebControls.ListBox lBoxEmp;
protected System.Web.UI.WebControls.DropDownList ddlEmp;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = DBCon.createConnection();
con.Open();
//显示部门
SqlCommand cmd = new SqlCommand("select * from TDepartment", con);//定义连接 SqlDataReader sdr = cmd.ExecuteReader();
this.ddlDep.DataSource = sdr;
this.ddlDep.DataTextField = "depName";
this.ddlDep.DataValueField = "depID";
this.ddlDep.DataBind();
sdr.Close();
//显示员工
SqlCommand cmdEmp = new SqlCommand("select * from emp where depID=" + this.ddlDep.SelectedValue, con);
SqlDataReader sdrEmp=cmdEmp.ExecuteReader();
while (sdrEmp.Read())
{
this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1), sdrEmp.GetInt32(0).ToString()));
sdrEmp.Close();
//关闭连接
con.Close();
}
}
}
就是SqlDataReader sdrEmp=cmdEmp.ExecuteReader();这行出错,阅读器关闭时尝试调用Read无效,堆栈
我添加了一个类,然后在WebForm1里拽了个DropDownList和一个ListBox,
在线等待ing...
--------------------编程问答-------------------- 眼睛看着都难受 --------------------编程问答-------------------- while (sdrEmp.Read())
{
this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1), sdrEmp.GetInt32(0).ToString()));
}
sdrEmp.Close();
con.Close(); --------------------编程问答-------------------- 额,不会吧~ --------------------编程问答-------------------- while (sdrEmp.Read())
{
this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1), sdrEmp.GetInt32(0).ToString()));
sdrEmp.Close();
//关闭连接
con.Close();
}
补充:.NET技术 , C#