-4

我正在尝试从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);
    }
}
4

2 回答 2

6

您的方法签名说它应该返回一个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);

你有两个选择。

  1. 将函数的返回类型更改为void. 这是可取的,因为您已经Literal在方法中设置了 's text 属性。
  2. 将函数的最后一部分更改为return FusionCharts.RenderChart(.....
于 2013-02-18T22:19:01.323 回答
3

您需要在您的方法中添加一个返回值:

选项 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()

因此您无需返回。

于 2013-02-18T22:20:14.993 回答