-2

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.

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

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?

    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;
}

I'm running this on button click event.

    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();
    }
}
  • 1
    What? Where's the line where you're getting the error? – itsme86 Dec 14 '16 at 15:46
  • cmd.Parameters.AddWithValue("@id", ddlsubnum.Items[1].Text); – Kadaw Tomtom Dec 14 '16 at 15:48
  • 1
    So you are trying to populate your SQL query with something selected by the user, but at that time the list is empty, so you get an error? Is that it? If the list is empty, what should populate the SQL query instead of what would have been the selection? – blaze_125 Dec 14 '16 at 15:50
  • Possible duplicate of [What is an IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-and-how-do-i-fix-it) – krillgar Dec 14 '16 at 15:52
  • This question makes no sense – Liam Dec 14 '16 at 15:53
  • Not sure why the down vote. I took the time to post my code and research this. No, the user doesn't select anything. The ddl is populated by a query and the ddlsubnum.Items[0].Text is used by the datatable dt to graph the data. – Kadaw Tomtom Dec 14 '16 at 15:57
  • Yup, trying my best to explain it, but yea don't bother. I'll close this thread. – Kadaw Tomtom Dec 14 '16 at 15:58

1 Answers1

2

Seems like we're missing something here... So all I can suggest at this time is this...

string forQRY = ddlsubnum.Items.Count > 0 ? ddlsubnum.Items[0].Text : "";//if the list has items, populate index 0 from list, otherwise empty string""
cmd.Parameters.AddWithValue("@id", forQRY);

I don't get how you expect to be able to select something that does not exist...

blaze_125
  • 2,262
  • 1
  • 9
  • 19