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 Microsoft.Reporting.WebForms; public partial class _StockMonitoring : System.Web.UI.UserControl { //A method that gets the stock categories in the database and add it in the DropDownList item list protected void getCategories() { //comDB is class where my query is being manipulated //I use DataTable as my DataSource //Note that the “StocksCls” is a class that composed of methods like the “GetItemCategories()”. //The method “GetItemCategories()” is where the data is being processed, it basically fetching the data from the database and return it as a datatable. StocksCls get_stock_categories = new StocksCls (); DataTable dt = get_stock_categories.GetItemCategories(); if (dt.Rows.Count > 0) { DropDownList1.Items.Add("Select Category"); DropDownList1.Items.Add("All"); for (int i = 0; i < dt.Rows.Count; i++) { string cat = dt.Rows[i]["CategoryName"].ToString(); DropDownList1.Items.Add(cat); } } } //A method that populates the ReportViwer control protected void populateReportViewer() { //A method that populates the ReportViwer control StocksCls result = new StocksCls (); DataTable dt = result.getItemsPerCategory(DropDownList1.SelectedItem.Text); ReportDataSource rds = new ReportDataSource("DataSet1_DataTable1", dt); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.DataSources.Add(rds); ReportViewer1.LocalReport.Refresh(); } //A method that populates the ReportViwer control if the user select “ALL” //"DataSet1_DataTable1" is the name of ReportDataSource we have created earlier in step 1 protected void getAllItems() { StocksCls items = new StocksCls (); DataTable dt = items.getAllItemPerCategory(); ReportDataSource rds = new ReportDataSource("DataSet1_DataTable1", dt); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.DataSources.Add(rds); ReportViewer1.LocalReport.Refresh(); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { getCategories(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { populateReportViewer(); if (DropDownList1.SelectedItem.Text == "All") { getAllItems(); } else { populateReportViewer(); } } } |