We help IT Professionals succeed at work.
Get Started

DropDownList retnring error OnSelectedIndexChanged

dodgerfan
dodgerfan asked
on
317 Views
Last Modified: 2012-06-27
I have an ASP.Net page for user input. I have 2 drop down lists. Once a selection is made from the first list, the values for the second list need to be populated based on that first selection. I am getting an error when you make the selection from the first list. It is an Object reference not set to an instance of an object. It appears that the second recordset that needs to be populated is empty. I have put in a reponse.write to ensure that there is a value selected, and it always displays the id selected. So I'm not sure where my code is going wrong. Any help is appreciated. My code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) BindLocations();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlLocation  = (DropDownList)PersonData.FindControl("ddlLocation ");
        DropDownList ddlOfficers = (DropDownList)PersonData.FindControl("ddlOfficers");

        using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConn"].ToString()))
        {
            ddlOfficers.DataTextField = "Last_Name";
            ddlOfficers.DataValueField = "UserID";
            SqlCommand cmd = new SqlCommand("SELECT UserID, LocationID, Last_Name FROM vw_Officers WHERE Location_ID = @LocationID", conn);
            cmd.Parameters.Add(new SqlParameter("@LocationID", ddlLocation.SelectedValue));
            conn.Open();
            ddlOfficers.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            ddlOfficers.Items.Clear();
            ddlOfficers.DataBind();
        }
        ddlOfficers.Enabled = true;
    }

    protected void BindLocations()
    {
        DropDownList ddlLocation = (DropDownList)PersonData.FindControl("ddlLocation");
        using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConn"].ToString()))
        {
            ddlLocation .DataTextField = "Name";
            ddlLocation .DataValueField = "LocationID";
            SqlCommand cmd = new SqlCommand("SELECT LocationID, Name, UserID FROM vw_Locations WHERE UserID = @UserID", conn);
            cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int).Value = SessionHelper.GetUserId();
            conn.Open();
            ddlLocation .DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            ddlLocation .DataBind();
        }
        ddlLocation .Items.Insert(0, new ListItem("SELECT A LOCATION", "-1"));
    }
}
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE