Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Render Crystal Report in different file formats without CrystalReportViewer

public static void RenderCrystalReport(DataSet ResultSet, String ReportLocation, String RenderType,HttpResponse Response)
        {
            MemoryStream oStream = new MemoryStream();
            ReportDocument crystalReport = new ReportDocument();
            crystalReport.Load(HttpContext.Current.Server.MapPath(ReportLocation));
            crystalReport.SetDataSource(ResultSet);

            switch (RenderType)
            {
                case "PDF":
                    oStream = crystalReport.ExportToStream(ExportFormatType.PortableDocFormat) as MemoryStream;
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = "application/pdf";
                    break;

                case "DOC":
                    oStream = crystalReport.ExportToStream(ExportFormatType.WordForWindows) as MemoryStream;
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = "application/doc";
                    break;

                case "XLS":
                    oStream = crystalReport.ExportToStream(ExportFormatType.Excel) as MemoryStream;
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = "application/vnd.ms-excel";
                    break;

            }


            Response.BinaryWrite(oStream.ToArray());
            Response.End();
        }

How to Check if URL is in live by C#

Web-based applications involve pages connected by links. Therefore, verifying the authenticity of links becomes a crucial factor. For example, one situation may involve verifyng a link before storing it into a database. Pass to the function a url as a string and it will return true (the link works) or false (the link did not work).

public bool checkUrlLink(string url) { HttpWebRequest request = HttpWebRequest)WebRequest.Create(url); request.Proxy = null; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return true; } catch { return false; } }
This code snippet uses HttpWebRequest and HttpWebResponse functions. Therefore, the snippet above requires System.Net namespace. Also note that this code snippet will also check wheather a url is properly formatted. If the format is not correct, it will return false (simply because the url did not receive a response).