Error in bind datalist with value of ddl

advertisements

I had ddl in datalist and when I tried to bind datalist with value from ddl this error apeared (object refrence not set ...) here DDLProduct.SelectedIndex

public DropDownList DDLProduct;

    protected void Page_Load(object sender, EventArgs e)
    {
     }

 protected void DLProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;
            DDLProduct.Items.Insert(0, new ListItem("Swithch Model", "0"));
        }

    }
  protected void DDlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLProduct.SelectedIndex > 0)
        {
            using
            (SqlConnection conn = Connection.GetConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_GetProductsByProductID";
                SqlParameter ParentID_Param = cmd.Parameters.Add("@ProductID", SqlDbType.Int);
                ParentID_Param.Value = DDLProduct.SelectedValue;
                ;
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
                DLProduct.DataSource = dt;
                DLProduct.DataBind();

            }
        }
    }


I think your problem is here:

DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;

Whats the purpose of this line?

You already have a reference to this control, "DDLProduct"

edit - without the actual exception ect I'm only guessing.