Friday, October 29, 2021

Creating a Bar chart and grid in asp.net with dummy data and with web service - How to pass dummy data to grid

 First create a class Employee

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace WebApplication4

{

    public class Employee

    {

        public int empID { get; set; }

        public string empName { get; set; }

        public int empAge { get; set; }


        //int[] i = new int[] { 1,21,6,5,5};

    }

}


Than create a web service with sample data

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;


namespace WebApplication4

{

    /// <summary>

    /// Summary description for WebService1

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

    // [System.Web.Script.Services.ScriptService]

    public class WebService1 : System.Web.Services.WebService

    {


        [WebMethod]

        public List<Employee> HelloWorld()

        {

            List<Employee> lstEmp = new List<Employee>();

            lstEmp.Add(new Employee() { empName = "yash", empAge = 32, empID = 2 });

            lstEmp.Add(new Employee() { empName = "yash2", empAge = 45, empID = 4 });

            lstEmp.Add(new Employee() { empName = "yash3", empAge = 32, empID = 6 });

            lstEmp.Add(new Employee() { empName = "yash4", empAge = 32, empID = 7 });

            lstEmp.Add(new Employee() { empName = "yash5", empAge = 32, empID = 8 });

            lstEmp.Add(new Employee() { empName = "yash6", empAge = 32, empID = 9 });

            lstEmp.Add(new Employee() { empName = "yash7", empAge = 32, empID = 10 });

            lstEmp.Add(new Employee() { empName = "yash8", empAge = 32, empID = 11 });

            lstEmp.Add(new Employee() { empName = "yash9", empAge = 32, empID = 12 });

            lstEmp.Add(new Employee() { empName = "yash10", empAge = 32, empID = 13 });

            lstEmp.Add(new Employee() { empName = "yash11", empAge = 32, empID = 14 });

            lstEmp.Add(new Employee() { empName = "yash12", empAge = 32, empID = 15 });

            lstEmp.Add(new Employee() { empName = "yash13", empAge = 32, empID = 16 });

            lstEmp.Add(new Employee() { empName = "yash4", empAge = 32, empID = 17 });

            lstEmp.Add(new Employee() { empName = "yash4", empAge = 32, empID = 18 });

            return lstEmp;

        }

    }

}


Than create grid and chart in aspx page

<asp:GridView ID="GridView1" runat="server">

        <Columns><asp:BoundField DataField="empID" HeaderText="ID" /> 

            <asp:BoundField DataField="empName" HeaderText="Name" /> 

            <asp:BoundField DataField="empAge" HeaderText="Age" /> 


        </Columns>


    </asp:GridView>


    <asp:Chart ID="Chart1" runat="server" Width="500px">

        <Series>

            <asp:Series Name="Series1" XValueMember="empName" YValueMembers="empAge"></asp:Series>

        </Series>

        <ChartAreas>

            <asp:ChartArea Name="ChartArea1"></asp:ChartArea>

        </ChartAreas>

    </asp:Chart>


Than call the webservice in aspx.cs page

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


namespace WebApplication4

{

    public partial class Grid : Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            WebService1 w = new WebService1();

            List<Employee> emp = w.HelloWorld();



            GridView1.DataSource = emp;

            GridView1.DataBind();


            Chart1.DataSource = emp;

        }

    }

}


references:- https://www.c-sharpcorner.com/uploadfile/63f5c2/chart-control-in-asp-net/


If we get error chart Http Handler than add below code to web.config

<appSettings>

  <add key="ChartImageHandler" value="storage=file;timeout=20;" />

</appSettings>

<compilation targetFramework="4.0">

 <assemblies>

  <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

 </assemblies>

</compilation>

<system.webServer>

<handlers>

      <add name="ChartImg" verb="*" path="ChartImg.axd"  type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  />

    </handlers>

</system.webServer>

references:- https://stackoverflow.com/questions/2117102/mscharts-no-http-handler-was-found-for-request-type-get-error

https://www.c-sharpcorner.com/UploadFile/77a82c/binding-gridview-using-list-in-Asp-Net/

https://forums.codeguru.com/showthread.php?359656-How-to-get-a-dummy-column-in-a-datagrid

https://github.com/digitalcolony/asp-net-gridview-dummy-data/blob/master/load-grid-view.aspx.cs

No comments:

Post a Comment