Tuesday, September 28, 2010

How To Create Line Chart in Asp.net

add an handler and add following code
==========================

using System;
using System.Web;
using IndiamartLineChart;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public class hdlLineChart : IHttpHandler   {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/png";
        string ChartVal = context.Request.QueryString["CV"];
        MemoryStream memStream = new MemoryStream();
        Bitmap b;
        LineChart c = new LineChart(600, 300);
        c.Title = "Progress Line Chart";
        c.Xorigin = 29;
        c.ScaleX = 10; c.Xdivs = 10;
        c.Yorigin = 0; c.ScaleY = 10; c.Ydivs = 10;
        if (ChartVal != string.Empty)
        {           
            int stval = 0, endval=0;
            for (int CmnI = 0; CmnI < ChartVal.Split('x').Length - 1; CmnI++)
            {
                stval =  Convert.ToInt32(ChartVal.Split('x')[CmnI].Split(',')[0]);
                endval =  Convert.ToInt32(ChartVal.Split('x')[CmnI].Split(',')[1]);
                if (c.Xorigin != 29) c.Xorigin = stval;
                c.AddValue(stval,endval);
            }
        }
        b = c.Draw();
        b.Save(memStream, ImageFormat.Png);
        memStream.WriteTo(context.Response.OutputStream);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
  
}


use this in app_code or create a class libray
=============================
public class LineChart
    {
        public LineChart()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public Bitmap b;

        public string Title = "Progress Line Chart";

        public ArrayList chartValues = new ArrayList();

        public float Xorigin = 0, Yorigin = 0;

        public float ScaleX, ScaleY;

        public float Xdivs = 2, Ydivs = 2;

        private int Width, Height;

        private Graphics g;

        //private Page p;

        public LineChart(int myWidth, int myHeight)
        {

            Width = myWidth; Height = myHeight;

            ScaleX = myWidth; ScaleY = myHeight;

            b = new Bitmap(myWidth, myHeight);

            g = Graphics.FromImage(b);

            //p = myPage;

        }
        struct datapoint
        {

            public float x;

            public float y;

            public bool valid;

        }

        public void AddValue(int x, int y)
        {

            datapoint myPoint;

            myPoint.x = x;

            myPoint.y = y;

            myPoint.valid = true;

            chartValues.Add(myPoint);

        }



        public Bitmap Draw()
        {

            int i;

            float x, y, x0, y0;

            string myLabel;

            Pen blackPen = new Pen(Color.YellowGreen, 1);

            Brush blackBrush = new SolidBrush(Color.YellowGreen);

            Font axesFont = new Font("arial", 10);



            //first establish working area

            //p.Response.ContentType = "image/jpeg";

            g.FillRectangle(new SolidBrush(Color.Transparent), 0, 0, Width, Height);
            //g.DrawRectangle(new Pen(Color.Black), 0, 0, Width, Height);
            int ChartInset = 50;

            int ChartWidth = Width - (2 * ChartInset);

            int ChartHeight = Height - (2 * ChartInset);//2

            g.DrawRectangle(new Pen(Color.Blue, 1), ChartInset, ChartInset, ChartWidth, ChartHeight);



            //must draw all text items before doing the rotate below

            g.DrawString(Title, new Font("arial", 14), blackBrush, Width / 3, 10);

            //draw X axis labels

            for (i = 0; i <= Xdivs; i++)
            {

                x = ChartInset + (i * ChartWidth) / Xdivs;

                y = ChartHeight + ChartInset;

                myLabel = (Xorigin + (ScaleX * i / Xdivs)).ToString();

                g.DrawString(myLabel, axesFont, blackBrush, x - 4, y + 10);

                g.DrawLine(blackPen, x, y + 2, x, y - 2);

            }

            //draw Y axis labels

            for (i = 0; i <= Ydivs; i++)
            {

                x = ChartInset;

                y = ChartHeight + ChartInset - (i * ChartHeight / Ydivs);

                myLabel = (Yorigin + (ScaleY * i / Ydivs)).ToString();

                g.DrawString(myLabel, axesFont, blackBrush, 5, y - 2);//6

                g.DrawLine(blackPen, x + 2, y, x - 2, y);

            }



            //transform drawing coords to lower-left (0,0)

            g.RotateTransform(180);

            g.TranslateTransform(0, -Height);

            g.TranslateTransform(-ChartInset, ChartInset);

            g.ScaleTransform(-1, 1);



            //draw chart data

            datapoint prevPoint = new datapoint();

            prevPoint.valid = false;

            foreach (datapoint myPoint in chartValues)
            {

                if (prevPoint.valid == true)
                {

                    x0 = ChartWidth * (prevPoint.x - Xorigin) / ScaleX;

                    y0 = ChartHeight * (prevPoint.y - Yorigin) / ScaleY;

                    x = ChartWidth * (myPoint.x - Xorigin) / ScaleX;

                    y = ChartHeight * (myPoint.y - Yorigin) / ScaleY;

                    g.DrawLine(blackPen, x0, y0, x, y);

                    g.FillEllipse(blackBrush, x0 - 2, y0 - 2, 4, 4);

                    g.FillEllipse(blackBrush, x - 2, y - 2, 4, 4);

                }

                prevPoint = myPoint;

            }



            //finally send graphics to browser
            return b;
            //MemoryStream memStream = new MemoryStream();
            // b.Save(memStream, ImageFormat.Jpeg);

        }

        ~LineChart()
        {

            g.Dispose();

            b.Dispose();

        }



    }

Sunday, September 19, 2010

How to get Error from application

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim ErrStr As String
        ErrStr = "Some Error Has Occured in Websit" & vbCrLf
        ErrStr &= "Error Information Are Given Below" & vbCrLf
        ErrStr &= "---------------------------------------" & vbCrLf
        strError &= Server.GetLastError().ToString  +  vbCrLf
        SendMail("naween@gmail.com", "naween@live.com", "UnHandeled Exception Error", ErrStr)
 End Sub

How to Export DataGrid or GridView to PDF

Download dll from link 
http://sourceforge.net/projects/itextsharp/ 
 
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

protected void Button5_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        gvEmployee.AllowPaging = false; //gvEmployee is name of GridView
        gvEmployee.DataBind();
        gvEmployee.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();  
        
    }

How to Export DataGrid or GridView to Text

StringBuilder str = new StringBuilder();

for(int i=0;i<=ds.Tables[0].Rows.Count - 1; i++)
{
  for(int j=0;j<=ds.Tables[0].Columns.Count - 1; j++)
  {
      str.Append(ds.Tables[0].Rows[i][j].ToString());
  }

  str.Append("
");
}

Response.Clear();
Response.AddHeader("content-disposition", 
         "attachment;filename=FileName.txt");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.text";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = 
              new HtmlTextWriter(stringWrite);

Response.Write(str.ToString());
Response.End();

How to Export DataGrid or GridView to Word

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End()

How to Export DataGrid or Gridview to Excel

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();