我使用asp图表控件在图表上显示数据库中的一些值。但是,我面临以下错误,我已通过单行注释指定。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select distinct name from tblDars";
DataTable dt = GetData(query);
ddlSubject.DataSource = dt;
ddlSubject.DataTextField = "tblDars";
ddlSubject.DataValueField = "tblDars";
//ddlSubject.DataBind();
ddlSubject.Items.Insert(0, new ListItem("Select", ""));
}
}
控件的标记如下:
<asp:DropDownList ID="ddlSubject" runat="server"
onselectedindexchanged="ddlSubjectChanged" AutoPostBack = "true">
</asp:DropDownList><hr />
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px" Visible = "false">
<Titles>
<asp:Title ShadowOffset="3" Name="Items" />
</Titles>
<Legends>
<asp:Legend Alignment="Center" Docking="Top" IsTextAutoFit="False" Name="Default" LegendStyle="Row" />
</Legends>
<Series>
<asp:Series Name="درس" />
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0" />
</ChartAreas>
</asp:Chart>
我在课堂上还有两个其他功能;
一个用于事件处理,另一个用于执行数据库作业;它们如下所示:
protected void ddlSubjectChanged(object sender, EventArgs e)
{
Chart1.Visible = ddlSubject.SelectedValue != "";
//string query = string.Format("select shipcity, count(orderid) from orders where shipcountry = '{0}' group by shipcity", ddlSubject.SelectedValue);
string query = string.Format("select Name, count (id) from tblDars where Name='{0}' group by Name", ddlSubject.SelectedValue);
DataTable dt = GetData(query);
string[] x = new string[dt.Rows.Count];
int[] y = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
Chart1.Series[0].Points.DataBindXY(x, y);
Chart1.Series[0].ChartType = SeriesChartType.Bar;
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
Chart1.Legends[0].Enabled = true;
}
private static DataTable GetData(string query)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(query);
String constr = ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
一切似乎都好我不知道错误来自何处。
答案 0 :(得分:0)
你回来&#34;名字&#34; db中的列,但您要设置无效的列名
在DataTextField和DataValueField中指定有效列。
ddlSubject.DataTextField = "name";
ddlSubject.DataValueField = "name";