Monday, May 20, 2019

How to display the Pie Chart using C# Asp.Net

Here Parshotam has explained with an example and attached sample code, how to programmatically populate ASP.Net Pie Chart from SQL Server Database Table.


In this example, the Pie is chart is dynamically populated based on DropDownList selection


You will need to modify the Web.Config file as following shown in YELLOW in order to use the ASP.Net 4.0 Chart control.

<appSettings>
  <add key="ChartImageHandler" value="storage=file;timeout=20;" />
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.0">
   <assemblies>
     <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
   </assemblies>
  </compilation>
  <httpHandlers>
       <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
  </httpHandlers>
  <pages>
   <controls>
                <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
   </controls>
  </pages>
</system.web>
<system.webServer>
 <handlers>
  <remove name="ChartImageHandler"/>
            <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" 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>


User Interface Code as below : 


<asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged"
    AutoPostBack = "true">
</asp:DropDownList><hr />
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px" Visible = "false">
    <Titles>
        <asp:Title ShadowOffset="3" Name="Items" />
    </Titles>
    <Legends>
        <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default" LegendStyle="Row" />
    </Legends>
    <Series>
        <asp:Series Name="Default" />
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1" BorderWidth="0" />
    </ChartAreas>
</asp:Chart>

Code Behind :
You will need to import the following Namespaces.

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string query = "select distinct shipcountry from orders";
        DataTable dt = GetData(query);
        ddlCountries.DataSource = dt;
        ddlCountries.DataTextField = "shipcountry";
        ddlCountries.DataValueField = "shipcountry";
        ddlCountries.DataBind();
        ddlCountries.Items.Insert(0, new ListItem("Select"""));
    }
}
   
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
    Chart1.Visible = ddlCountries.SelectedValue != "";
    string query = string.Format("select shipcity, count(orderid) from orders where shipcountry = '{0}' group by shipcity", ddlCountries.SelectedValue);
    DataTable dt = GetData(query);
    string[] x = new string[dt.Rows.Count];
    int[] y = new int[dt.Rows.Count];
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        x[i] = dt.Rows[i][0].ToString();
        y[i] = Convert.ToInt32(dt.Rows[i][1]);
    }
    Chart1.Series[0].Points.DataBindXY(x, y);
    Chart1.Series[0].ChartType = SeriesChartType.Pie;
    Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
    Chart1.Legends[0].Enabled = true;
}
 
private static DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand(query);
    String constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    return dt;
}

Output as below in Pie Chart


Connect to ERP system with .Net Application

Hi everyone !!!!!

here is the example to connect the SAP ERP syetm to .Net Application.

for connection to SAP ERP to .NET Application through RFC (Remote Function), we need some dll files that can be easily available in internet or microsoft

SAPLogonCtrl.dll
SAPFunctionsOCX.dll

Step 1 : Download these 2 files

Step 2 : Open visual studio --> Start New Project (using C# or VB)

Step 3: In the solution explorer 'Right Click', add reference to these dll into you website

Step 4 : After add reference, need to write the code for connect to ERP SAP

Step 5 : Write below code to connect to ERP using namespace


using SAPLogonCtrl;
using SAPFunctionsOCX;


protected Boolean SAPConnection()
    {  

        SAPLogonCtrl.SAPLogonControlClass Login = new SAPLogonCtrl.SAPLogonControlClass();
        SAPLogonCtrl.Connection SapConn;

        Login.ApplicationServer = "ERP Server Name or IP";
        Login.SystemNumber = SystemNumber;
        Login.Client = "ClientNumber";
        Login.Language = "SAP Language";
        Login.User = "SAP User ID";
        Login.Password = "SAP Password";
        SapConn = (SAPLogonCtrl.Connection)Login.NewConnection();
        if (SapConn.Logon(0, true))
        {
            Response.Write("Connected to SAP");
            return true;
        }
        else
        {
            Response.Write("Not able to connect SAP");
            return false;
        }
    }