Hey Priyanka59,
Please refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table>
<tr>
<td align="center">
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px">
<Titles>
<asp:Title ShadowOffset="3" Name="Items" />
</Titles>
<Legends>
<asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
LegendStyle="Row" />
</Legends>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0" />
</ChartAreas>
</asp:Chart>
</td>
</tr>
<tr>
<td align="center">
<asp:GridView ID="gvCustomers" runat="server">
</asp:GridView>
</td>
</tr>
</tr>
</table>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
//Fetch the Statistical data from database.
string query = "SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total]";
query += " FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil')";
query += " GROUP BY ShipCountry, DATEPART(Year, OrderDate)";
DataTable dt = GetData(query);
//Get the DISTINCT Countries.
List<string> countries = (from p in dt.AsEnumerable()
select p.Field<string>("ShipCountry")).Distinct().ToList();
//Loop through the Countries.
foreach (string country in countries)
{
//Get the Year for each Country.
int[] x = (from p in dt.AsEnumerable()
where p.Field<string>("ShipCountry") == country
orderby p.Field<int>("Year") ascending
select p.Field<int>("Year")).ToArray();
//Get the Total of Orders for each Country.
int[] y = (from p in dt.AsEnumerable()
where p.Field<string>("ShipCountry") == country
orderby p.Field<int>("Year") ascending
select p.Field<int>("Total")).ToArray();
//Add Series to the Chart.
Chart1.Series.Add(new Series(country));
Chart1.Series[country].IsValueShownAsLabel = true;
Chart1.Series[country].ChartType = SeriesChartType.Column;
Chart1.Series[country].Points.DataBindXY(x, y);
}
Chart1.Legends[0].Enabled = false;
}
}
private void BindGrid()
{
string query = @"SELECT * FROM
(
SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total]
FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil')
GROUP BY ShipCountry, DATEPART(Year, OrderDate)
) as tbl
PIVOT(MAX(Total) FOR Year IN ([1996],[1997],[1998])) as PVT";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
gvCustomers.HeaderRow.Cells[0].Text = "";
}
}
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
'Fetch the Statistical data from database.
Dim query As String = "SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total]"
query += " FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil')"
query += " GROUP BY ShipCountry, DATEPART(Year, OrderDate)"
Dim dt As DataTable = GetData(query)
'Get the DISTINCT Countries.
Dim countries As List(Of String) = (From p In dt.AsEnumerable() _
Select p.Field(Of String)("ShipCountry")).Distinct().ToList()
'Loop through the Countries.
For Each country As String In countries
'Get the Year for each Country.
Dim x As Integer() = (From p In dt.AsEnumerable() _
Where p.Field(Of String)("ShipCountry") = country _
Order By p.Field(Of Integer)("Year") _
Select p.Field(Of Integer)("Year")).ToArray()
'Get the Total of Orders for each Country.
Dim y As Integer() = (From p In dt.AsEnumerable() _
Where p.Field(Of String)("ShipCountry") = country _
Order By p.Field(Of Integer)("Year") _
Select p.Field(Of Integer)("Total")).ToArray()
'Add Series to the Chart.
Chart1.Series.Add(New Series(country))
Chart1.Series(country).IsValueShownAsLabel = True
Chart1.Series(country).ChartType = SeriesChartType.Column
Chart1.Series(country).Points.DataBindXY(x, y)
Next
Chart1.Legends(0).Enabled = True
End If
End Sub
Private Sub BindGrid()
Dim query As String = "SELECT * FROM(SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil') GROUP BY ShipCountry, DATEPART(Year, OrderDate)) as tbl PIVOT(MAX(Total) FOR Year IN ([1996],[1997],[1998])) as PVT "
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
gvCustomers.HeaderRow.Cells(0).Text = ""
End Using
End Using
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Function
Screenshot