Wednesday, October 8, 2014

Export tables, div, Grids and any to Excel and PDF in asp.net c#

Export tables, div, Grids and any to Excel and PDF in asp.net c#

àWhile exporting  these designs with out images it will working fine

à If it having images means you need to give the absolute path for the image URL then only it will work

Check this blog

 Excel Export
protected void imgexcel_Click(object sender, ImageClickEventArgs e)
        {

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=Suppliers.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            tblrpt.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();

            //Response.Clear();
            //Response.Buffer = true;
            //Response.AddHeader("content-disposition",
            //  "attachment;filename=GridViewExport.doc");
            //Response.Charset = "";
            //Response.ContentType = "application/vnd.ms-word ";
            //StringWriter sw = new StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(sw);
            //imgReportHeading.Width = 160;
            //imgReportHeading.Height = 42;
            //tblrpt.RenderControl(hw);
            //Response.Output.Write(sw.ToString());
            //Response.Flush();
            //Response.End();
        }

Pdf Export
protected void imgpdf_Click(object sender, ImageClickEventArgs 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);

            tblrpt.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();
        }


Read More »

How to save the Image to folder from data base in asp.net

How to save the Image to folder from data base in asp.net

1)  By using this method getting the image bytes and save to the folder

public void SaveImageToFolder()
        {
            try
            {
                DataTable dt = new DataTable();
                byte[] byts = null;
                string strcon = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand command = new SqlCommand("select COMPANY_LOGO from COMPANYIMAGES where COMPANY_SYSID='" + GPRSTrackSession.CompanyCode + "'", connection);
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    Byte[] bytes = (Byte[])dt.Rows[0]["COMPANY_LOGO"];

                    var pathName = "~/Images/rptimgs.jpg";
                    var fileName = Server.MapPath(pathName);

                    FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                    fileStream.Write(bytes, 0, bytes.Length);
                    fileStream.Close();
                }
            }
            catch (Exception ex)
            {

            }

        }

2) And assign that path to the image url in the application
string imgPath = Request.Url.AbsoluteUri.Split('/')[0].ToString() + "//" + Request.Url.AbsoluteUri.Split('/')[2] + "/images/rptimgs.jpg";
                            imgReportHeading.ImageUrl = imgPath;




Read More »

Monday, September 15, 2014

Writing the log file from asp.net

Step1:- Copy these two methods into code file and access the those methods in your project where you want


public void ExceptionHandler(string ExceptionMessage)
        {
            try
            {
                // string root = Directory.GetCurrentDirectory() + "\\Log";
                // if (!Directory.Exists(root))
                // {
                // Directory.CreateDirectory(root);
                // }

                string root = "C:\\Log\\";
                string path = root + "SampleLog" + System.DateTime.Now.ToString("ddMMyyy") + ".txt";

                CallExceptionHandler(path, ExceptionMessage);

            }
            catch (Exception)
            { }
        }
Read More »

Wednesday, July 23, 2014

Split the string using the XML data type in the SQL server

Introduction

In this article we going to discuss about how to split the string and convert in to the XML format and from that insert in to on temp table

Create one sample input variable

DECLARE @sampleText varchar(500)='lokesh1,lokesh2,lokesh3,lokesh4,lokesh5,lokesh6,lokesh7,lokesh8,lokesh9,lokesh10'

And create one sample variable with XML data type like  
In this XML data type I assumed <lok></lok> as  the tag notation
Declare @XML XML
set @XML=CAST('<lok>'+REPLACE(@sampleText,',','</lok><lok>')+'</lok>' as XML)

And create one temp table like

DECLARE @temp AS TABLE 
( 
      rownum INT IDENTITY(1, 1), 
      name   VARCHAR(50) 
)

Read More »

Wednesday, July 16, 2014

Binding the Asp.Net grid view using the JQUERY with ajax call to the Web Method

 Introduction

         In this article we are going to know how to bind the server side DataSet data to the grid view

         First create a sample dynamic dataset

         From that convert that data set data to the XML format in the static web method

        Then write the client side code to call that method using the ajax json call in the JQUERY

        Add the JQUERY script plug in’s shown below

        In Asp.net project create one .js file name it as “ASPSnippets_Pager.min.js “


Read More »