why chart not showing in my old asp.net application

RAVI 1,076 Reputation points
2023-10-26T12:48:35.8233333+00:00

Hello

This is my aspx old page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>jQuery Google Pie Chart Example in asp.net</title>

<script src="http://code.jquery.com/jquery-1.8.2.js"></script>

<script src="http://www.google.com/jsapi" type="text/javascript"></script>

<script type="text/javascript">

// Global variable to hold data

google.load('visualization', '1', { packages: ['corechart'] });

</script>

<script type="text/javascript">

$(function () {

$.ajax({

type: 'POST',

dataType: 'json',

contentType: 'application/json',

url: 'Default2.aspx/GetChartData',

data: '{}',

success:

function (response) {

drawchart(response.d);

},

error: function () {

alert("Error loading data! Please try again.");

}

});

})

function drawchart(dataValues) {

var data = new google.visualization.DataTable();

data.addColumn('string', 'Column Name');

data.addColumn('number', 'Column Value');

for (var i = 0; i < dataValues.length; i++) {

data.addRow([dataValues[i].Countryname, dataValues[i].Total]);

}

new google.visualization.PieChart(document.getElementById('chartdiv')).

draw(data, { title: "Show Google Chart in Asp.net" });

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div id="chartdiv" style="width: 600px; height: 350px;">

</div>

</form>

</body>

</html>

This is my c# code

using System;
 using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

public partial class Default2 : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static List<countrydetails> GetChartData()
    {
        DataTable dt = new DataTable();
         SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString);
         con1.Open();
         SqlCommand cmd = new SqlCommand("select distinct name as name,value from Table_1", con1);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            con1.Close();
        
            List<countrydetails> dataList = new List<countrydetails>();
            foreach (DataRow dtrow in dt.Rows)
            {
                countrydetails details = new countrydetails();
                details.countryname = dtrow[0].ToString();
                details.total = Convert.ToInt32(dtrow[1]);
                dataList.Add(details);
            }
            return dataList;

        }

    public class countrydetails
    {
        private string Countryname;
        private int Total;

        public string countryname
        {
            get
            {
                return Countryname;
            }
            set
            {
                Countryname = value;
            }
        }

        public int total
        {
            get
            {
                return Total;
            }
            set
            {
                Total = value;
            }
        }

    }

}

This is my datbase table

 
/****** Object:  Table [dbo].[Table_1]    Script Date: 10/26/2023 18:18:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_1](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](50) NULL,
	[value] [int] NULL,
 CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Table_1] ON
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (1, N'aa', 10)
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (2, N'bb', 20)
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (3, N'cc', 30)
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (4, N'dd', 40)
SET IDENTITY_INSERT [dbo].[Table_1] OFF

on loading its not showing chart but no error

Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-10-31T09:41:37.37+00:00

    Hi @RAVI

    After my modifications to your code, I implemented your needs. This is full of complex data type conversions.

    1.Open RouteConfig.cs

    Picture1

    Modify the code:

    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Off;
            routes.EnableFriendlyUrls(settings);
        }
    }
    
    
    

    2.Modify the front-end code

    <html>
     <head>
    
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
         <script type="text/javascript" src="Scripts/jquery-3.4.1.js"></script>
    <script type="text/javascript">
        google.charts.load('current', { 'packages': ['corechart'] });
        google.charts.setOnLoadCallback(drawChart);
        $(function () {
    
    
    
            $.ajax({
                type: "Post",
                url: "Default2.aspx.aspx/GetChartData",
              
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success:
                    function (response) {
                       
                        drawChart(response.d);
                        
                    },
            });
        })
        function drawChart(dataValues) {
           
            var data = new google.visualization.DataTable();
            var list = [];
            data.addColumn('string', 'Column Name');
    
            data.addColumn('number', 'Column Value');
            dataValues.forEach((item) => {
                var value = [item.countryname, item.total];
                list.push(value);
            });
            data.addRows(list);
            var options = {
                'title': 'Show Google Chart in Asp.net',
                'width': 400,
                'height': 300
            };    
            var chart = new google.visualization.PieChart(document.getElementById('chartdiv'));
            chart.draw(data, options);
        }
    </script>
    </head>
    
    <body>
    
    <form id="form1" runat="server">
    
    <div id="chartdiv" style="width: 600px; height: 350px;">
    
    </div>
    
    </form>
    
    </body>
    </html>
    
    
    

    Output:

    Picture3

    Main problem: The parameter of data.addrows is list[list].

    Best regards,

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. RAVI 1,076 Reputation points
    2023-10-30T05:28:05.5866667+00:00

    @QiYou-MSFT

    Can you please give me chart example for older version of asp.net using my html c# and sql table data please

    Thanking You

    0 comments No comments

  2. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-10-30T07:38:50.33+00:00

    Hi @RAVI

    Modify the code in RouteConfig .cs

     settings.AutoRedirectMode = RedirectMode.Off;
    
    
    

    Add a jquery file:

    <script src="Scripts/jquery-3.4.1.min.js"></script>

    You can bring the back-end data to the front-end through AJAX.

    3

    As For the Google API, I'm not very familiar with it, I'm very sorry.

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. RAVI 1,076 Reputation points
    2023-10-30T07:49:07.19+00:00

    Can you please help me with one things

    copy my database table i want to show that table data in chart using simple solution using asp.net c#

    please make one simple example

    thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.