locked
binding array to dropdownlist RRS feed

  • Question

  • im trying to do a simple bind of a string array to a dropdownlist but keep getting the error

    "Object reference not set to an instance of an object." on ddl.DataSource = ddlMyStrings;

    what am i missing?

    -------------------

    codebehind:

    string[] myStrings = new string[] {"string1", "string2", "string3"};

    DropDownList ddl = new DropDownList();

    ddl = (DropDownList)e.Row.FindControl("ddlMyStrings");

    ddl.DataSource = myStrings;

    ddl.DataBind();

    -----------------------------

    aspx code:

    <asp:TemplateField HeaderText="MyStrings">

    <ItemTemplate>

    <asp:DropDownList ID="ddlMyStrings" AutoPostBack="true" runat="server"></asp:DropDownList>

    </ItemTemplate>

    <ItemStyle HorizontalAlign="Center" />

    </asp:TemplateField>

    Friday, July 21, 2006 10:48 AM

Answers

  • well as it turns out, i solved my own problem...or maybe i didnt post enough code for someone else to have a fair shot at it.....  anyway, the the solution lay in looking for the ddl in the right row type.  The first row to bind is the header row, which doesnt contain a ddl...thus the null reference.  after wrapping an if statement around the block testing for DataControlRowType.DataRow, all is well with the world.

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

         string[] myStrings = new string[] {"string1", "string2", "string3"};

         DropDownList ddl = new DropDownList();

         ddl = (DropDownList)e.Row.FindControl("ddlMyStrings");

         ddl.DataSource = myStrings;

         ddl.DataBind();

    }

     

    "sometimes i find that if i just sit down, the solution presents itself"   --dr. henry jones sr.

    Saturday, July 22, 2006 5:01 AM

All replies

  • You have possible error in code

    string[] myStrings = new string[] {"string1", "string2", "string3"};

    DropDownList ddl = new DropDownList();

    ddl = (DropDownList)e.Row.FindControl("ddlMyStrings"); // Probably here ddl is set to null as control with this name could not be found

    ddl.DataSource = ddlMyStrings;

    ddl.DataBind()




    As this is template control you have to search in parent control instead of page.Controls collection
    Friday, July 21, 2006 11:52 AM
  • If im understand this correctly and you want to take the strings and place them into a drop down list then there is an easier way. Atleast i feel make a combo box instead because you can use it like a dropdown box for instance i took you code and then i show you what i would do

    YOUR CODE:

    codebehind:

    string[] myStrings = new string[] {"string1", "string2", "string3"};

    DropDownList ddl = new DropDownList();

    ddl = (DropDownList)e.Row.FindControl("ddlMyStrings");

    ddl.DataSource = myStrings;

    ddl.DataBind();

     

    My MODIFIED CODE:

    codebehind:

    string[] myStrings = new string[] {"string1", "string2", "string3"};

    ComboBox ddl = new ComboBox();

    ddl.DropDownStyle = ComboBoxStyle.DropDownList; // stops the user from being able to delete the info in the box and forces them to selct something. Howvwr this i optional

    ddl.Items.AddRange(myStrings); // now this will store it as objecdt so when you pull it out u have to type cast it back to a string

    Friday, July 21, 2006 5:56 PM
  • hi,

    for webpages questions you can ask in this forum http://forums.asp.net

    hope this helps

    Friday, July 21, 2006 6:57 PM
  • well as it turns out, i solved my own problem...or maybe i didnt post enough code for someone else to have a fair shot at it.....  anyway, the the solution lay in looking for the ddl in the right row type.  The first row to bind is the header row, which doesnt contain a ddl...thus the null reference.  after wrapping an if statement around the block testing for DataControlRowType.DataRow, all is well with the world.

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

         string[] myStrings = new string[] {"string1", "string2", "string3"};

         DropDownList ddl = new DropDownList();

         ddl = (DropDownList)e.Row.FindControl("ddlMyStrings");

         ddl.DataSource = myStrings;

         ddl.DataBind();

    }

     

    "sometimes i find that if i just sit down, the solution presents itself"   --dr. henry jones sr.

    Saturday, July 22, 2006 5:01 AM
  • Ice Romeo, Your answer is correct for a WinForm.  However, the original poster was talking about a WebForm.
    Sunday, July 23, 2006 2:23 PM
  • Write this on page load....

     

    DropDownList1.DataSource = Arrayname;

    DropDownList1.DataBind(); 

     

    Thursday, January 27, 2011 9:27 AM