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