C# 条形图ASP.net C的输入字符串格式不正确
我试图使用图表显示摘要页面,但似乎有错误,我不知道如何解决它。当我运行SQL语句时,数据将显示如下 |总计|学生响应| |2 | A| |0 | B| |1 | C| |3 | D|C# 条形图ASP.net C的输入字符串格式不正确,c#,asp.net,charts,C#,Asp.net,Charts,我试图使用图表显示摘要页面,但似乎有错误,我不知道如何解决它。当我运行SQL语句时,数据将显示如下 |总计|学生响应| |2 | A| |0 | B| |1 | C| |3 | D| using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MySql.Data.My
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
public partial class Teacher_Summary : System.Web.UI.Page
{
MySqlConnection myConn;
protected void Page_Load(object sender, EventArgs e)
{
chartQn1.Visible = false;
int quiz = (int)Session["topicID"];
string question1 = (string)Session["question1"];
myConn.Open();
DataTable dt = new DataTable();
string populateData = "select count(studentResponse) as Total, studentResponse from result where quizID = '" + quiz + "' and questionID = '" + question1 + "' group by studentResponse";
MySqlDataAdapter sda = new MySqlDataAdapter(populateData, myConn);
sda.Fill(dt);
myConn.Close();
string[] x = new string[dt.Rows.Count];
double[] y = new double[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToDouble(dt.Rows[i][1]); // <-- this is the error
}
chartQn1.Series[0].Points.DataBindXY(x, y);
chartQn1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
}
protected void btnDone_Click(object sender, EventArgs e)
{
Response.Redirect("../Teacher/Questions.aspx");
}
protected void btnReveal_Click(object sender, EventArgs e)
{
btnReveal.Visible = false;
chartQn1.Visible = true;
}
protected void chartQn1_Load(object sender, EventArgs e)
{
}
}
那我该怎么做呢?
我收到的错误是Y的输入错误。检查您的类型
y[i] = Convert.ToDouble(dt.Rows[i][1]);
您试图将字符串“A”转换为double和其他“B”、“C”、“D”首先,我想您可能已经改变了顺序,请尝试x[I]=dt.Rows[I][1];和y[i]=Convert.ToDoubledt.Rows[i][0];。第二,考虑使用方法,它增加错误检查,并确保如果代码无效,则代码不会崩溃。