Search code examples
c#asp.netrepeater

Getting "Specified argument was out of the range of valid values. Parameter name:"


I know there are a tons of this title on several posts but having browsed through some of them, I have not found any the helps with my issue.

I am trying to build a dropdownlist in Repeater control that is dynamically populated with data from the database.

Here is the code I am using:

//markup

State:

//codefile

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconstring"].ToString());
        string sSQL = "Select stateID, sateName from Mytable ORDER By stateName ASC";
            SqlCommand cmd6 = new SqlCommand(sSQL, con);
            con.Open();
            SqlDataReader dtrClient = cmd6.ExecuteReader();
            DropDownList ddl = (DropDownList)Repeater2.Controls[Repeater2.Controls.Count - 1].FindControl("ddlstate");
            ddl.DataSource = dtrClient;
            ddl.DataTextField = "stateName";
            ddl.DataValueField = "stateID";
            ddl.DataBind();

When I run it, I get the following error message:

Specified argument was out of the range of valid values. Parameter name: index

on the following line:

DropDownList ddl = (DropDownList)Repeater2.Controls[Repeater2.Controls.Count - 1].FindControl("ddlstate");

Any ideas how to resolve this?

UPDATE:

     State: <asp:DropDownList ID="aircraftstate" runat="server" style="width:150px;" AppendDataBoundItems="True">
     <asp:ListItem Value="" Selected="True"></asp:ListItem>
     </asp:DropDownList> 



    //We query the DB only once in the Page Load
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
     string sSQL = "Select StateID, StateName from MyTable ORDER By sName ASC";
     SqlCommand cmd3 = new SqlCommand(sSQL, con);
     con.Open();
     table = new DataTable();
     table.Load(cmd3.ExecuteReader());

   //We load the DropDownList in the event
    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var ddl = (DropDownList)e.Item.FindControl("aircraftstate");
        ddl.DataSource = table;
        ddl.DataTextField = "StateName";
        ddl.DataValueField = "StateID";
        ddl.DataBind();

Solution

  • As a Repeater is a templated based control you should Find your DropDownList and populate it in the ItemDataBound event. So you could do something like this:

    DataTable table;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //We query the DB only once in the Page Load
        string sSQL = "Select stateID, sateName from Mytable ORDER By stateName ASC";
        SqlCommand cmd6 = new SqlCommand(sSQL, con);
        con.Open();
        table = new DataTable();
        table.Load(cmd6.ExecuteReader());
    
    }
    
    //We load the DropDownList in the event
    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var ddl = (DropDownList)e.Item.FindControl("ddlID");
        ddl.DataSource = table;
        ddl.DataTextField = "stateName";
        ddl.DataValueField = "stateID";
        ddl.DataBind();
    
    }