Wednesday, March 26, 2014

How To Use MySQL Database connection in asp.net

1) add mysql.data dll to your project and add namespace using MySql.Data.MySqlClient;

2) MySqlConnection myConnection = new MySqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=FAcustomer;Integrated Security=True");

3) protected void BindChart()
        {
            int DoubleExpectedReturn = 10;//Convert.ToInt32(Request.QueryString["annualinterest"]);
            DoubleExpectedReturn = DoubleExpectedReturn * 2;
            int PortFolioYear = 2014;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            myConnection.Open();
            string cmdstr = @"SELECT DirectEquity, EquityMF, GSec, IncomeMF, Gold, FII
                         FROM  msttbInvestmentPortfolio
                         WHERE (DoubleExpectedReturn = " + DoubleExpectedReturn + ") AND (YEAR(portfolioYear) = " + PortFolioYear + ")";
            MySqlCommand cmd = new MySqlCommand(cmdstr, myConnection);
            MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
            adp.Fill(ds);
            dt = ds.Tables[0];

            string[] a = { "Direct Equity", "Equity MF", "G Sec", "Income MF", "Gold ETF", "FII" };
            string[] x = new string[dt.Columns.Count];
            double[] y = new double[dt.Columns.Count];
            LegendItem item = new LegendItem();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (Convert.ToDouble(dt.Rows[0][i]) != 0.0)
                {
                    x[i] = a[i];//dt.Rows[0][i].ToString(); a[i];//
                    y[i] = Convert.ToDouble(dt.Rows[0][i]);

                }
            }
            Chart1.Series[0].PostBackValue = "#AXISLABEL,#INDEX";
            Chart1.Series[0].LegendPostBackValue = "#AXISLABEL,#INDEX";
            Chart1.Series[0].Points.DataBindXY(x, y);
            //new
            //Chart1.Series[0]["PointWidth"] = "0.5";
            //Chart1.Series[0]["DrawingStyle"] = "Cylinder";
            //Chart1.Series[0]["BarLabelStyle"] = "Center";
            //Chart1.Series[0]["LineColor"] = "Black";
            //Chart1.Series[0]["LabelStyle"] = "Outside";
            Chart1.Series[0].ToolTip = "#AXISLABEL";          
            //Chart1.Series[0].ShadowColor = Color.Black;
            // Chart1.Series[0].Palette = ChartColorPalette.Chocolate;


            //end of new
            Chart1.Series[0].Label = "#PERCENT";//Shows Percentage on Pie Chart
            Chart1.Series[0].LabelForeColor = Color.White;
            Chart1.Series[0].Font = new Font("Arial", 10f);
            Chart1.Series[0].LegendText = "#AXISLABEL";//Shows Text In Legend

            Chart1.Series[0].IsVisibleInLegend = true;
            Chart1.Series[0].ChartType = SeriesChartType.Pie;
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;

            Chart1.Legends[0].Font = new Font("Candara", 11f);

            myConnection.Close();
            //Chart1.Legends[0].Enabled = true;

            // Chart1.Series[0]["PieLabelStyle"] = "Disabled";



        }

Create PIE chart using asp.net

1) add This to aspx Page--- <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

2) add System.web.datavisualization dll to references folder

3) Add To ASPX Page :

<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px" OnClick="Chart1_Click">
                                    <Titles>
                                        <asp:Title ShadowOffset="3" Name="Items" />
                                    </Titles>
                                    <Series>
                                        <asp:Series Name="Default" />
                                    </Series>
                                    <ChartAreas>
                                        <asp:ChartArea Name="ChartArea1" BorderWidth="0">
                                       
                                        <AxisX>
                                            <MajorGrid Enabled="False" />
                                        </AxisX>
                                        <AxisY>
                                            <MajorGrid Enabled="False" />
                                        </AxisY>
                                        </asp:ChartArea>
                                    </ChartAreas>
                                   
                                    <Legends>
                                        <asp:Legend Alignment="Center" Docking="Right" IsDockedInsideChartArea="false" IsTextAutoFit="true"
                                            Name="Default" LegendStyle="Table">
                                        </asp:Legend>
                                    </Legends>
                                </asp:Chart>
4) On code Page :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

 SqlConnection myConnection = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=FAcustomer;Integrated Security=True");

 protected void Page_Load(object sender, EventArgs e)
        {
            this.Chart1.Click += new ImageMapEventHandler(this.Chart1_Click);

            if (!IsPostBack)
            {
                BindChart();
            }
        }
  protected void BindChart()
        {
            int DoubleExpectedReturn = 10;//Convert.ToInt32(Request.QueryString["annualinterest"]);
            DoubleExpectedReturn = DoubleExpectedReturn * 2;
            int PortFolioYear = 2014;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            myConnection.Open();
            string cmdstr = @"SELECT DirectEquity, EquityMF, GSec, IncomeMF, Gold, FII
                         FROM  msttbInvestmentPortfolio
                         WHERE (DoubleExpectedReturn = " + DoubleExpectedReturn + ") AND (YEAR(portfolioYear) = " + PortFolioYear + ")";
            SqlCommand cmd = new SqlCommand(cmdstr, myConnection);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            dt = ds.Tables[0];

            string[] a = { "Direct Equity", "Equity MF", "G Sec", "Income MF", "Gold ETF", "FII" };
            string[] x = new string[dt.Columns.Count];
            double[] y = new double[dt.Columns.Count];
            LegendItem item = new LegendItem();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (Convert.ToDouble(dt.Rows[0][i]) != 0.0)
                {
                    x[i] = a[i];//dt.Rows[0][i].ToString(); a[i];//
                    y[i] = Convert.ToDouble(dt.Rows[0][i]);

                }
            }
            Chart1.Series[0].PostBackValue = "#AXISLABEL,#INDEX";
            Chart1.Series[0].LegendPostBackValue = "#AXISLABEL,#INDEX";
            Chart1.Series[0].Points.DataBindXY(x, y);
            //new
            //Chart1.Series[0]["PointWidth"] = "0.5";
            //Chart1.Series[0]["DrawingStyle"] = "Cylinder";
            //Chart1.Series[0]["BarLabelStyle"] = "Center";
            //Chart1.Series[0]["LineColor"] = "Black";
            //Chart1.Series[0]["LabelStyle"] = "Outside";
            Chart1.Series[0].ToolTip = "#AXISLABEL";          
            //Chart1.Series[0].ShadowColor = Color.Black;
            // Chart1.Series[0].Palette = ChartColorPalette.Chocolate;


            //end of new
            Chart1.Series[0].Label = "#PERCENT";//Shows Percentage on Pie Chart
            Chart1.Series[0].LabelForeColor = Color.White;
            Chart1.Series[0].Font = new Font("Arial", 10f);
            Chart1.Series[0].LegendText = "#AXISLABEL";//Shows Text In Legend

            Chart1.Series[0].IsVisibleInLegend = true;
            Chart1.Series[0].ChartType = SeriesChartType.Pie;
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;

            Chart1.Legends[0].Font = new Font("Candara", 11f);

            myConnection.Close();
            //Chart1.Legends[0].Enabled = true;

            // Chart1.Series[0]["PieLabelStyle"] = "Disabled";



        }

 protected void Chart1_Click(object sender, ImageMapEventArgs e)
        {
            string[] Values = e.PostBackValue.Split(',');
            string InstrumentName, Index;
            InstrumentName = Values[0];
            Index = Values[1];
            gvInstrument.Columns[0].HeaderText = InstrumentName + " Instrument List";
            BindChart();

       

        }

Pen drive show empty even when data exists

1) Click on "Start" -->Run --> Type cmd and press Enter. 

2) Enter this command : attrib -h -r -s /s /d g:\*.* --> Press Enter   *(Replace the letter g with your pen drive drivename.)

3)Now check your pen drive for the files. 

Open New Tab On ASP.NET Button Click with Response.Redirect

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick ="document.forms[0].target = '_blank';" Text="FA" />