I am creating a website that acts as a form, but includes two cascading drop down lists (so one feeds from the other). I have used the following example to create my lists (I am fairly new to c# .net development so needed to work through a tutorial)
I have changed all the relevant areas of code to point to the right tables and database for my query. When I run the application I am getting an issue with Configuration Manager. I looked this up and followed the instructions in this feed: The name 'ConfigurationManager' does not exist in the current context
I hit f5 to run in visual studio and got a slightly different error of a non legal operation, so applied the advice here:
Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state
After all of this I am still getting the following:
Exception User-Unhandled System.NullReferenceException: 'Object reference not set to an instance of an object' System.Configuration.ConnectionStringSettingCollection.this[string].get returned null.
Here is my code:
<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();
}
}
}