-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);

2つのオプションがあります。

  1. 関数の戻り値のタイプをに変更しますvoidLiteralメソッド内で'stextプロパティをすでに設定しているため、これは望ましいことです。
  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 に答える