我有一个由以下查询填充的下拉列表。该列表从不是静态的,因此列表中的项目总数将不同。
<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %> " SelectCommand="SELECT * FROM TABLE WHERE STATUS = 'done' "></asp:SqlDataSource>
我知道我试图在我的下拉列表中访问一个不存在的索引。是否存在以避免选择不存在的索引?
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;
}
我在按钮点击事件上运行此功能。
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();
}
}
答案 0 :(得分:2)
好像我们在这里遗漏了一些东西......所以我现在可以建议的是......
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);
我不明白你期望能够选择一些不存在的东西......