Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Tue Mar 03, 2020 7:45 pm

Hello, the pie chart example shows how to set hard-coded values. On your site, /Tutorials/Spire.Presentation/Spire.Presentation-Program-Guide/Chart/How-to-Create-Pie-Chart-in-PowerPoint-in-C.html I was going over.

What I am trying to do is generate the chart using a SQL Server Database View. This view returns two columns as a top 5, Name and Count. This is my code.

When I try filling the data, it blows up on me.

Code: Select all
Connection string:
            SqlConnection sqlconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);


Code: Select all
            string sqlQueryTop5High = "SELECT [Name], [Count] FROM v_Highs;";


Code: Select all
                SqlCommand sqlcmdTop5High = new SqlCommand(sqlQueryTop5High, sqlconn);
                using (SqlDataAdapter da3 = new SqlDataAdapter(sqlcmdTop5High))
                    try
                    {
                        DataTable dt3 = new DataTable();
                        da3.Fill(dt3); // This is where it dies.
                        InitChartData(chart3, dt3);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.ToString());
                    }
                    finally
                    {
                        sqlconn.Close();
                    }

And this is the code that is supposed to generate the chart data
Code: Select all
        #region InitChartData Helper
        private static void InitChartData(IChart chart1, DataTable dt1)
        {
            //set series name
            ChartSeriesFormatCollection ctf = chart1.Series;
            for (int c = 0; c < dt1.Columns.Count; c++)
            {
                chart1.ChartData[0, c].Text = dt1.Columns[c].Caption;
                if (c > 0)
                {
                    ctf.Append(chart1.ChartData[0, c]);
                }
            }
            ChartCategoryCollection catg = chart1.Categories;
            for (int r = 0; r < dt1.Rows.Count; r++)
            {
                //set category
                catg.Append(chart1.ChartData[r + 1, 0]);

                //set chartData value
                object[] data = dt1.Rows[r].ItemArray;

                for (int c = 0; c < data.Length; c++)
                {
                    chart1.ChartData[r + 1, c].Value = data[c];
                }
                //set series value
                for (int i = 0; i < ctf.Count; i++)
                {
                    chart1.Series[i].Values.Add(chart1.ChartData[r + 1, i + 1]);
                }
            }
            for (int i = 0; i < chart1.Series.Count; i++)
            {
                ChartSeriesDataFormat series = chart1.Series[i];
                for (int j = 0; j < series.Values.Count; j++)
                {
                    ChartDataLabel label = series.DataLabels.Add();
                    label.ID = j;
                    label.LabelValueVisible = true;
                    label.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
                    label.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
                }
            }
        }
        #endregion

What am I doing wrong???

dbrewerton-1
 
Posts: 7
Joined: Tue Mar 03, 2020 7:40 pm

Wed Mar 04, 2020 5:57 am

Hello,

Thanks for your inquiry.
According to your description, you should have encountered some problems when exporting data from the database to the datatable.
Sorry this doesn't belong to the features of our Spire.Presentation. Please check if there is any problem with your database connection or configuration.
If you have other questions when using our product, please feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Mar 06, 2020 1:55 pm

There was an issue with the database connection, that's been resolved. Thank you.

dbrewerton-1
 
Posts: 7
Joined: Tue Mar 03, 2020 7:40 pm

Mon Mar 09, 2020 1:38 am

Hello,

Glad to hear that you found the cause yourself.
If you need further assistance, just feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Presentation