我在页面上添加了两个DropDwonList,一个是叫DDLDept,在Pageload时绑定数据库中的一个字段,另一个叫DDLRoom,与前一个DropDwonList联动,在第一个DropDwonList的SelectedIndexChanged事件中绑定所需字段中的数据,但是我调试的时候,选择第一个DropDwonList数据就出现错误,说:列名 '***' 无效。 不知道是什么原因,不知道哪位高手知道啊,帮我看看下面的程序哪里有问题,谢谢!主要代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
//页面初始化时绑定数据
string CnnString=System.Configuration.ConfigurationSettings.AppSettings["DBConnectionString"];
SqlConnection Connection = new SqlConnection(CnnString);
Connection.Open();
string cmdStr="select DeptName from System_Dept";
SqlCommand cmd=new SqlCommand(cmdStr,Connection);
SqlDataReader dr=cmd.ExecuteReader();
DDLDept.DataSource=dr;
DDLDept.DataTextField="DeptName";
DDLDept.DataValueField="DeptName";
DDLDept.DataBind();
dr.Close();
}
}private void DDLDept_SelectedIndexChanged(object sender, System.EventArgs e)
{
string CnnString=System.Configuration.ConfigurationSettings.AppSettings["DBConnectionString"];
SqlConnection Connection = new SqlConnection(CnnString);
Connection.Open();
string cmdStr="select RoomNo from ManagementExpense_DeptRoom where DeptName="+DDLDept.SelectedValue;
SqlCommand cmd=new SqlCommand(cmdStr,Connection);
SqlDataReader dr=cmd.ExecuteReader();
DDLRoom.DataSource=dr;
DDLRoom.DataTextField="RoomNo";
DDLRoom.DataValueField="RoomNo";
DDLRoom.DataBind();
dr.Close();
}