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";



        }

No comments:

Post a Comment