0

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)

https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx

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();
        }
        }
    }
MIS_Gem
  • 117
  • 9
  • Make sure to put the configuration in a web.config that gets deployed alongside your web application. – CodeCaster Jun 04 '18 at 12:41
  • You should add a reference to system.configuration – Igal23 Jun 04 '18 at 12:44
  • @Igal23 thank you, I have referenced "using System.Configuration" and have also right clicked on my project Add -- Reference and selected it here as well to make sure it is referenced. This has not solved the issue. – MIS_Gem Jun 04 '18 at 12:55
  • @CodeCaster Thank you, this is my web.config file : – MIS_Gem Jun 04 '18 at 12:58
  • Thank you - I realised my issue. I had code in both web.config and web.debug.config that was conflicting and I was calling the wrong name. Your advice helped me see my issue. – MIS_Gem Jun 04 '18 at 13:03

0 Answers0