ASP.NET C# Tutorial In Urdu - How to load data dynamically in chart control
Code is below.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Chart2
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
LoadDataInChart();
}
private void LoadDataInChart()
{
Chart1.DataSource = GetData();
Chart1.Series["Series1"].XValueMember = "Year";
Chart1.Series["Series1"].YValueMembers = "Total";
Chart1.DataBind();
}
private DataTable GetData()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbx"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("usp_Chart2", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Parameters.AddWithValue("@StartYear", StartYearDropDownList.SelectedValue);
cmd.Parameters.AddWithValue("@EndYear", EndYearDropDownList.SelectedValue);
SqlDataReader reader = cmd.ExecuteReader();
DataTable dtData = new DataTable();
dtData.Load(reader);
return dtData;
}
}
}
protected void GoButton_Click(object sender, EventArgs e)
{
LoadDataInChart();
}
}
}
Stored Procedure: usp_Chart2
CREATE PROCEDURE usp_Chart2
(
@StartYear NVARCHAR(50)
,@EndYear NVARCHAR(50)
)
AS
BEGIN
SELECT SUM(Total) AS 'Total'
,DATEPART(yyyy, DateOfOrder) AS 'Year'
FROM [dbo].[Orders]
GROUP BY DATEPART(yyyy, DateOfOrder)
HAVING DATEPART(yyyy, DateOfOrder) >= @StartYear AND DATEPART(yyyy, DateOfOrder) <= @EndYear
END
No comments:
Post a Comment