如何使用 C# 访问下拉列表中的选定值

本文关键字:下拉列表 何使用 访问 | 更新日期: 2023-09-27 17:55:42

我正在尝试访问下拉列表中的选定值并将该值传递到我的 select 语句中,所以我只能提取过滤后的数据,但我收到错误并且由于此问题而未生成图表。 这是错误:"错误:'pieYScale'为空或不是对象"。
我怀疑我在下拉列表中引用所选值的方式是错误的。

public string CreateChart()
    {
        string DDList = DropDownList1.SelectedValue;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
        string sqlStatement = "select   Status as GroupCategory, Count(Status) as TotalCount from  [MasterProject] where Closing_Date >=  '" + txtStartClosingDate.Text + "' and Closing_Date <= '" + txtEndClosingDate.Text + "' and GroupProject = '" + DDList + "' and Status is not null group by  Status Order by TotalCount Desc";
        SqlCommand cmd = new SqlCommand(sqlStatement, con);
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        string strXML;
        strXML = "<graph decimalPrecision='0' name='MyXScaleAnim' type='ANIMATION' duration='1' start='0' param='_xscale' showNames='1' labelDisplay='Rotate' useEllipsesWhenOverflow='1'  pieSliceDepth='30' formatNumberScale='0'>";
        while (reader.Read())
        {
            strXML += "<set name='" + reader["GroupCategory"].ToString() + "' value='" + reader["TotalCount"].ToString() + "' />";
        }
        strXML += "</graph>";
        return FusionCharts.RenderChart("../FusionCharts/Column3D.swf", "ChartID", strXML, "FactorySum", "500", "350", false, false);
    }

如何使用 C# 访问下拉列表中的选定值

撇开上面的代码完全容易受到SQL注入攻击等不谈,实际错误本身来自FusionCharts组件,似乎是一个错误,需要更新才能修复它。看这里

我已经使用这个修复了它:

 string DDList = DropDownList1.SelectedItem.Value;