Click here to Skip to main content
15,937,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one data table into sql server database and i want to show the data of that table using asp.net high chart from code behind file . how can i show the data of sql server data table into charts form.
Posted
Updated 25-Nov-17 0:05am

Hi,

Have a look at the following article:

Highcharts in asp.net using jquery ajax[^].

It provides a detailed explanation of how to use ASP.NET to show SQL Server data in High Charts.

... hope it helps.
 
Share this answer
 
static string conString = ConfigurationManager.ConnectionStrings["Connections"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<PieSeriesData> PieDataList = new List<PieSeriesData>();
                DataTable dt = piedatatable;

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        Double count = Convert.ToDouble(dr[CountColumn]);
                        String Name = Convert.ToString(dr[NameColumn]);
                        PieDataList.Add(new PieSeriesData { Name = Name, Y = count });
                    }


                    Highcharts higcharts = new Highcharts
                    {
                        Chart = new Chart
                        {
                            Type = ChartType.Pie,
                            Height = 350,
                            Options3d = new ChartOptions3d
                            {
                                Enabled = true,
                                Alpha = 25,
                                Beta = 0
                            }
                        },


                        Credits = { Enabled = false },
                        PlotOptions = new PlotOptions
                        {
                            Pie = new PlotOptionsPie
                            {
                                AllowPointSelect = true,
                                SlicedOffset = 20,
                                Cursor = PlotOptionsPieCursor.Pointer,
                                Depth = 45,
                                DataLabels = new PlotOptionsPieDataLabels
                                {
                                    Enabled = true,
                                    Format = "{point.name}<br/>{point.count}"
                                },
                                ShowInLegend = false


                            }

                        }

                        ,
                        Title = new Title
                        {
                            Text = pietittle,
                            X = -20
                        },
                        Legend = new Legend
                        {
                            Layout = LegendLayout.Horizontal,
                            Align = LegendAlign.Center,

                            BorderWidth = 0
                        },
                        Navigation = {
                        ButtonOptions=
                        {
                            Enabled= false
                        }
                    },
                        Series = new List<Series>
                {
                    new PieSeries
                    {
                        Name = pieseriesname,
                        Data = PieDataList as List<PieSeriesData>,
                      Size="80%"
                    }
                }
                    };
                    HighsoftNamespace Highsoft = new HighsoftNamespace();
                    string result = Highsoft.GetHighcharts(higcharts, "chart").ToHtmlString();
                    //Pichartseries.Text = result;
                    Response.Write(result);
                }
            }
        }
        public DataTable piedatatable { get; set; }
        public String pietittle { get; set; }
        public String pieseriesname { get; set; }
        public bool showinlegend { get; set; }
        public String CountColumn { get; set; }
        public String NameColumn { get; set; }
    }


This is how you can bind a pichart from code behind in asp.net highchart
 
Share this answer
 
v2
static string conStr = ConfigurationManager.ConnectionStrings["Connections"].ConnectionString;
        static string TokenID = string.Empty;
        AuthenticateToken oauth = new AuthenticateToken();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Crypto crypto = new Crypto();
                    string requestUrl = Request.Url.ToString();
                    string[] token = Regex.Split(requestUrl, "tokenId=");
                    if (!string.IsNullOrEmpty(token[1]))
                    {
                        TokenID = crypto.DecryptFromString(token[1]);
                        String SiteCode = GetComaSeperaredSiteCode(new Guid(TokenID));
                        DataTable dt = new DataTable();
                        dt = GetAssetTotal(SiteCode);
                        GridViewReports.DataSource = dt;
                        GridViewReports.DataBind();
                        Pichartcontrol.piedatatable = dt;
                        Pichartcontrol.pieseriesname = "MEP Total";
                        Pichartcontrol.pietittle = "Asset Total";
                        Pichartcontrol.NameColumn = "SiteCode";
                        Pichartcontrol.CountColumn = "TotalAssetCount";
                    }
                }
            }
            catch
            {
                Response.Redirect("~/Reports/Oops.aspx");
            }
        }
        #region Get Coma Seperated Site Code
        protected string GetComaSeperaredSiteCode(Guid TokenID)
        {
            string SiteCodes = string.Empty;
            try
            {
                if (oauth.ValidateTokenId(TokenID.ToString()))
                {
                    using (SqlConnection con = new SqlConnection(conStr))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.Connection = con;
                            if (con.State == ConnectionState.Closed)
                            {
                                con.Open();
                            }
                            cmd.CommandText = "[Intelligios].[UspGetSiteCodes]";
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@TokenID", TokenID);
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                            DataTable dt = new DataTable();
                            da.Fill(dt);
                            if (dt.Rows.Count > 0)
                            {
                                SiteCodes = String.Join(",", dt.AsEnumerable().Select(x => x.Field<string>("SiteCode").ToString()).ToArray());
                            }
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/Reports/Invalid.aspx", false);
                }
            }
            catch
            {
                Response.Redirect("~/Reports/Oops.aspx");
            }
            return SiteCodes;
        }
        #endregion
        #region Get Total Asset Count
        protected DataTable GetAssetTotal(string SiteCodes)
        {
            DataTable dt = new DataTable();
            try
            {
                if (oauth.ValidateTokenId(TokenID.ToString()))
                {
                    using (SqlConnection con = new SqlConnection(conStr))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.Connection = con;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "[Intelligios].[UspGetAssetTotal]";
                            cmd.Parameters.AddWithValue("@IsBME", 0);
                            cmd.Parameters.AddWithValue("@GroupCode", SiteCodes);
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                            da.Fill(dt);
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/Reports/Invalid.aspx", false);
                }
            }
            catch
            {
                Response.Redirect("~/Reports/Oops.aspx");
            }
            return dt;
        }
        #endregion
    }


and you need to populate the datatable using this code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900