connectseekers.com

Monday, 12 September 2016

Asp.Net Chart: Column Chart in Asp.net From DataBase Table Using C#.Net

9/12/2016 - By Pranav Singh 0

This article will show you how you can create a bar chart using asp.net chart control in asp.net and c#. This will take data in datatable from database.

So for this article first we will create a new asp.net application and add an asp.net chart control. And after that we will select the chart type as bar chart.


Your page code will look as shown below:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ColumnChart.aspx.cs" Inherits="WebApplication7.ColumnChart " %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Chart ID="Chart1" runat="server" Width="500px" Palette="Excel">
                <Series>
                    <asp:Series Name="Growth" XValueMember="Country" YValueMembers="Growth"
                        IsVisibleInLegend="true"
                        IsValueShownAsLabel="true" YValuesPerPoint="2">
                    </asp:Series>
                </Series>
                <ChartAreas>
                    <asp:ChartArea Name="ChartArea1">
                        <AxisX LineColor="Gray">
                            <MajorGrid LineColor="Gray" />
                        </AxisX>
                        <AxisY LineColor="Gray">
                            <MajorGrid LineColor="Gray" />
                        </AxisY>
                        <Area3DStyle Enable3D="false" LightStyle="Realistic"></Area3DStyle>
                    </asp:ChartArea>
                </ChartAreas>
                <Legends>
                    <asp:Legend>
                    </asp:Legend>
                </Legends>
            </asp:Chart>
        </div>
    </form>
</body>
</html>

After this we will create a datatable for passing the data to the chart to display.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class ColumnChart: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable _objdt = new DataTable();
            _objdt = GetDataForChart();
            BindDataToChartcontrol(_objdt);

        }
        ///
        /// This function will bind data to chart control
        ///
        public void BindDataToChartcontrol(DataTable _objdt)
        {
            Chart1.DataSource = _objdt;
            Chart1.DataBind();
        }
        ///
        /// This method will provide data
        /// In this methos you can fatch data from DB and pass it to chart control
        ///
        ///
        public DataTable GetDataForChart()
        {
            DataTable _objdt = new DataTable();
            SqlConnection con = new SqlConnection("--Your connectionstring--");
            SqlDataAdapter da = new SqlDataAdapter("Select * from Country", con);
            da.Fill(_objdt);
            return _objdt;
        }
    }
}

Now we have done run the application to check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top