Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index dropdownlist
<p>I have a dropdownlist that is populated by the below query.  The list is never static, so the number of items in the list will always be different.</p>

<pre><code>&lt;asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt; " SelectCommand="SELECT * FROM TABLE WHERE STATUS = 'done' "&gt;&lt;/asp:SqlDataSource&gt;
</code></pre>

<p>I know that i'm trying to access an index in my dropdownlist that doesn't exist.  Is there away to avoid selecting an index that does not exist?</p>

<pre><code>    private DataTable test()
{
    DataTable dt = new DataTable();
    SqlDataAdapter Adpt;

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
    {
        SqlCommand cmd = new SqlCommand("SELECT * FROM TABLE WHERE id=@id", con);
        cmd.Parameters.AddWithValue("@id", ddlsubnum.Items[0].Text);
        Adpt = new SqlDataAdapter(cmd);
        new SqlDataAdapter(cmd).Fill(dt);
    }
    Chart1.Series["Series1"].YValueMembers = "recs";
    Chart1.Series["Series1"].XValueMember = "date";
    Chart1.Series["Series1"].XValueType = ChartValueType.DateTime;
    Chart1.Series["Series1"].YValueType = ChartValueType.Int32;

    return dt;
}

private DataTable test2()
{
    DataTable dt2 = new DataTable();
    SqlDataAdapter Adpt;

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
    {
        SqlCommand cmd = new SqlCommand("SELECT * FROM TABLE WHERE id=@id", con);
        cmd.Parameters.AddWithValue("@id", ddlsubnum.Items[1].Text);
        Adpt = new SqlDataAdapter(cmd);
        new SqlDataAdapter(cmd).Fill(dt2);
    }
    Chart2.Series["Series1"].YValueMembers = "recs";
    Chart2.Series["Series1"].XValueMember = "date";
    Chart2.Series["Series1"].XValueType = ChartValueType.DateTime;
    Chart2.Series["Series1"].YValueType = ChartValueType.Int32;

    return dt2;
}
</code></pre>

<p>I'm running this on button click event.</p>

<pre><code>    protected void Button1_Click(object sender, EventArgs e)
{
    {
        DataTable dt = this.test();
        Chart1.DataSource = dt;
        Chart1.DataBind();
    }

    {
        DataTable dt2 = this.test2();
        Chart2.DataSource = dt2;
        Chart2.DataBind();
    }
}
</code></pre>