views:

215

answers:

2

This is probably a simple question but I am developing a web app in C# with DropDownList. Currently it is working for just one DropDownList. But now that I modified the code so that number of DropDownLists that should appear is dynamic, it gives me error;

"The name 'ddl' does not exist in the current context."

The reason for this error is that there a multiple instances of 'ddl' = number of counters. So how do I instead return more than one 'ddl'? Like what return type should this method have instead? And how do I return these values?

Reason I need it dynamic is I need to create one DropDownList for each column in whatever Adventureworks table they select.

      private DropDownList CreateDropDownLists()
    {
        for (int counter = 0; counter < NumberOfControls; counter++)
        {
            DropDownList ddl = new DropDownList();
            SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
            ddl.ID = "DropDownListID" + (counter + 1).ToString();

            int NumControls = targettable.Length;
            DataTable dt = new DataTable();
            dt.Load(dr2);

            ddl.DataValueField = "COLUMN_NAME";
            ddl.DataTextField = "COLUMN_NAME";
            ddl.DataSource = dt;
            ddl.ID = "DropDownListID 1";
            ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
            ddl.DataBind();

            ddl.AutoPostBack = true;
            ddl.EnableViewState = true; //Preserves View State info on Postbacks
            //ddlList.Style["position"] = "absolute";
            //ddl.Style["top"] = 80 + "px";
            //ddl.Style["left"] = 0 + "px";
            dr2.Close();
        }
        return ddl;
    }
+1  A: 

Looks like you are trying to return the drop down at the end of the function, but its declared inside the scope of the for loop. Try creating an array of drop downs at the beginning of the function (before the for loop), and set the array inside the loops then return it. You'll also need to change your function declaration to support an array of drop downs.

private DropDownList[] CreateDropDownLists()
{
    DropDownList[] dropDowns = new DropDownList[NumberOfControls];

    for (int counter = 0; counter < NumberOfControls; counter++)
    {
        DropDownList ddl = new DropDownList();
        SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
        ddl.ID = "DropDownListID" + (counter + 1).ToString();

        int NumControls = targettable.Length;
        DataTable dt = new DataTable();
        dt.Load(dr2);

        ddl.DataValueField = "COLUMN_NAME";
        ddl.DataTextField = "COLUMN_NAME";
        ddl.DataSource = dt;
        ddl.ID = "DropDownListID 1";
        ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
        ddl.DataBind();

        ddl.AutoPostBack = true;
        ddl.EnableViewState = true; //Preserves View State info on Postbacks
        //ddlList.Style["position"] = "absolute";
        //ddl.Style["top"] = 80 + "px";
        //ddl.Style["left"] = 0 + "px";
        dr2.Close();

        dropDowns[counter] = ddl;
    }
    return dropDowns;
}
Kevin Wiskia
Great answer! I used this solution, Kevin!
salvationishere
+1  A: 

Why not just return a list of DropDownList?

private List<DropDownList> CreateDropDownLists()
{
    List<DropDownList> listDDL = new List<DropDownList>();
    for (int counter = 0; counter < NumberOfControls; counter++)
    {
        DropDownList ddl = new DropDownList();

        SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
        ddl.ID = "DropDownListID" + (counter + 1).ToString();

        int NumControls = targettable.Length;
        DataTable dt = new DataTable();
        dt.Load(dr2);

        ddl.DataValueField = "COLUMN_NAME";
        ddl.DataTextField = "COLUMN_NAME";
        ddl.DataSource = dt;
        ddl.ID = "DropDownListID 1";
        ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
        ddl.DataBind();

        ddl.AutoPostBack = true;
        ddl.EnableViewState = true; //Preserves View State info on Postbacks
        //ddlList.Style["position"] = "absolute";
        //ddl.Style["top"] = 80 + "px";
        //ddl.Style["left"] = 0 + "px";
        dr2.Close();

        listDDL.Add(ddl);
    }
    return listDDL;
}
sunpech
You can put DataReader in a using block as a best practice.
ydobonmai
Great answer, John. Better than what I had!
salvationishere
@salvationishere technically mine, john simply cleaned up the formatting. @Ashish good point, I only wanted to point out that a list could be used instead.
sunpech