我正在创建一个充当表单的网站,但包含两个级联下拉列表(因此一个来自另一个)。我使用以下示例来创建我的列表(我对c#.net开发很新,所以需要通过教程)
我已将所有相关的代码区域更改为指向我的查询的正确表和数据库。当我运行应用程序时,我遇到了Configuration Manager的问题。我查看了这个并按照此Feed中的说明操作: The name 'ConfigurationManager' does not exist in the current context
我点击f5在visual studio中运行并且得到了一个略有不同的非法律操作错误,所以在这里应用了建议:
Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state
在完成所有这些之后,我仍然得到以下内容:
异常用户未处理 System.NullReferenceException:'对象引用未设置为对象的实例' System.Configuration.ConnectionStringSettingCollection.this [string] .get返回null。
这是我的代码:
<connectionStrings>
<clear/>
<add name="conString"
connectionString="Data Source=CATE01-SRV-05;
Initial catalogue=iSAMS;Integrated Security=true"/>
</connectionStrings>
public partial class CoCurricular : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CategorySelect.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
String strQuery = "select TblActivityManagerFolderID, txtName from dbo.TblActivityManagerFolder";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
CategorySelect.DataSource = cmd.ExecuteReader();
CategorySelect.DataTextField = "txtName";
CategorySelect.DataValueField = "TblActivityManagerFolderID";
CategorySelect.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ActivitySelect.Items.Clear();
ActivitySelect.Items.Add(new ListItem("--Select Activity--", ""));
ActivitySelect.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
String strQuery = "select txtName from dbo.TblActivityManagerGroup " +
"where intFolder=@TblActivityManagerFolderID";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@TblActivityManagerFolderID",
CategorySelect.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ActivitySelect.DataSource = cmd.ExecuteReader();
ActivitySelect.DataTextField = "txtName";
ActivitySelect.DataValueField = "TblActivityManagerGroupID";
ActivitySelect.DataBind();
if (ActivitySelect.Items.Count > 1)
{
ActivitySelect.Enabled = true;
}
else
{
ActivitySelect.Enabled = false;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}