并不是所有的代码路径都返回C#中的值错误
本文关键字:错误 返回 路径 代码 并不是 | 更新日期: 2023-09-27 18:00:06
我正试图从Page_Load
事件加载我的图表,但收到错误"并非所有代码路径都返回值",我不确定我在这里做错了什么。有人能帮忙吗。
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
Literal2.Text = CreateChart_2();
}
public string CreateChart_2()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
// Initialize the string which would contain the chart data in XML format
StringBuilder xmlStr = new StringBuilder();
// Provide the relevant customization attributes to the chart
xmlStr.Append("<chart decimalPrecision='0' showShadow='1' showborder='1' caption='Number of Lots Assigned (YTD)' subcaption='" + result1 + "' name='MyXScaleAnim' type='ANIMATION' duration='1' start='0' param='_xscale' showNames='1' labelDisplay='Rotate' useEllipsesWhenOverflow='1' formatNumberScale='0'>");
{
// Establish the connection with the database
string sqlStatement = "SELECT count (ID)as TotalCount, cat_name FROM MyTable group by cat_name";
SqlCommand cmd = new SqlCommand(sqlStatement, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
// Begin iterating through the result set
//SqlDataReader rst = query.ExecuteReader();
while (reader.Read())
{
// Construct the chart data in XML format
xmlStr.AppendFormat("<set label='{0}' value='{1}' link='{2}'/>", reader["cat_name"].ToString(), reader["TotalCount"].ToString(), Server.UrlEncode("DrillDown1.aspx?AppName=" + reader["cat_name"].ToString()));
}
// End the XML string
xmlStr.Append("</chart>");
// Close the result set Reader object and the Connection object
reader.Close();
con.Close();
// Set the rendering mode to JavaScript, from the default Flash.
FusionCharts.SetRenderer("javascript");
// Call the RenderChart method, pass the correct parameters, and write the return value to the Literal tag
Literal2.Text = FusionCharts.RenderChart(
"FusionChartsXT/Column3D.swf", // Path to chart's SWF
"", // Page which returns chart data. Leave blank when using Data String.
xmlStr.ToString(), // String containing the chart data. Leave blank when using Data URL.
"annual_revenue", // Unique chart ID
"640", "340", // Width & Height of chart
false, // Disable Debug Mode
true);
}
}
您的方法签名表明它应该返回一个string
。。但是您在任何地方都没有return
语句。
您的呼叫站点是:
Literal2.Text = CreateChart_2();
但你在你的功能中这样做:
Literal2.Text = FusionCharts.RenderChart(
"FusionChartsXT/Column3D.swf", // Path to chart's SWF
"", // Page which returns chart data. Leave blank when using Data String.
xmlStr.ToString(), // String containing the chart data. Leave blank when using Data URL.
"annual_revenue", // Unique chart ID
"640", "340", // Width & Height of chart
false, // Disable Debug Mode
true);
你有两个选择。
- 将函数的返回类型更改为
void
。这是可取的,因为您已经在方法中设置了Literal
的text属性 - 将函数的最后一部分更改为
return FusionCharts.RenderChart(....
您需要在方法中添加一个返回:
选项1-返回
protected void Page_Load(object sender, EventArgs e)
{
Literal2.Text = CreateChart_2();
}
public string CreateChart_2()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
// Initialize the string which would contain the chart data in XML format
StringBuilder xmlStr = new StringBuilder();
// Provide the relevant customization attributes to the chart
xmlStr.Append("<chart decimalPrecision='0' showShadow='1' showborder='1' caption='Number of Lots Assigned (YTD)' subcaption='" + result1 + "' name='MyXScaleAnim' type='ANIMATION' duration='1' start='0' param='_xscale' showNames='1' labelDisplay='Rotate' useEllipsesWhenOverflow='1' formatNumberScale='0'>");
{
// Establish the connection with the database
string sqlStatement = "SELECT count (ID)as TotalCount, cat_name FROM MyTable group by cat_name";
SqlCommand cmd = new SqlCommand(sqlStatement, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
// Begin iterating through the result set
//SqlDataReader rst = query.ExecuteReader();
while (reader.Read())
{
// Construct the chart data in XML format
xmlStr.AppendFormat("<set label='{0}' value='{1}' link='{2}'/>", reader["cat_name"].ToString(), reader["TotalCount"].ToString(), Server.UrlEncode("DrillDown1.aspx?AppName=" + reader["cat_name"].ToString()));
}
// End the XML string
xmlStr.Append("</chart>");
// Close the result set Reader object and the Connection object
reader.Close();
con.Close();
// Set the rendering mode to JavaScript, from the default Flash.
FusionCharts.SetRenderer("javascript");
// Call the RenderChart method, pass the correct parameters, and write the return value to the Literal tag
Literal2.Text = FusionCharts.RenderChart(
"FusionChartsXT/Column3D.swf", // Path to chart's SWF
"", // Page which returns chart data. Leave blank when using Data String.
xmlStr.ToString(), // String containing the chart data. Leave blank when using Data URL.
"annual_revenue", // Unique chart ID
"640", "340", // Width & Height of chart
false, // Disable Debug Mode
true);
}
return xmlStr.ToString();
}
选项2-无回报
protected void Page_Load(object sender, EventArgs e)
{
CreateChart_2();
}
public void CreateChart_2()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
// Initialize the string which would contain the chart data in XML format
StringBuilder xmlStr = new StringBuilder();
// Provide the relevant customization attributes to the chart
xmlStr.Append("<chart decimalPrecision='0' showShadow='1' showborder='1' caption='Number of Lots Assigned (YTD)' subcaption='" + result1 + "' name='MyXScaleAnim' type='ANIMATION' duration='1' start='0' param='_xscale' showNames='1' labelDisplay='Rotate' useEllipsesWhenOverflow='1' formatNumberScale='0'>");
{
// Establish the connection with the database
string sqlStatement = "SELECT count (ID)as TotalCount, cat_name FROM MyTable group by cat_name";
SqlCommand cmd = new SqlCommand(sqlStatement, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
// Begin iterating through the result set
//SqlDataReader rst = query.ExecuteReader();
while (reader.Read())
{
// Construct the chart data in XML format
xmlStr.AppendFormat("<set label='{0}' value='{1}' link='{2}'/>", reader["cat_name"].ToString(), reader["TotalCount"].ToString(), Server.UrlEncode("DrillDown1.aspx?AppName=" + reader["cat_name"].ToString()));
}
// End the XML string
xmlStr.Append("</chart>");
// Close the result set Reader object and the Connection object
reader.Close();
con.Close();
// Set the rendering mode to JavaScript, from the default Flash.
FusionCharts.SetRenderer("javascript");
// Call the RenderChart method, pass the correct parameters, and write the return value to the Literal tag
Literal2.Text = FusionCharts.RenderChart(
"FusionChartsXT/Column3D.swf", // Path to chart's SWF
"", // Page which returns chart data. Leave blank when using Data String.
xmlStr.ToString(), // String containing the chart data. Leave blank when using Data URL.
"annual_revenue", // Unique chart ID
"640", "340", // Width & Height of chart
false, // Disable Debug Mode
true);
}
}
或者,如果您不想从CreateChart_2()
返回任何内容,则可以将签名更改为:
public void CreateChart_2()
因此您不需要返回。