Showing posts with label General. Show all posts
Showing posts with label General. Show all posts

Monday, May 6, 2013

how to export gridview data to pdf in asp.net

Title: How to export grid view data to PDF in asp.net

Description:
Most the report are send to customers as in excel format.But some scenarios they might ask PDF format.

Example:
Earlier we learned how to export grid view data to word document,Import excel data to SQL Server.In this post i will show how to export grid view data to PDF file in asp.net.While exporting to pdf i have created a pdf document with desired size .Then conversion has done using HTML parser
Note:itextsharp tool has been used.so we need add those DLL into our application
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportToPDF.aspx.cs" Inherits="_ExportToPDF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Export Gridview To PDF documnet</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvExportDetails" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EnableModelValidation="True"
>
<Columns>
<asp:BoundField DataField="ExportOrderID"
                 HeaderText="ExportOrderID" SortExpression="ExportOrderID"/>
<asp:BoundField DataField="ExportOrderName"
                HeaderText="ExportOrderName" SortExpression="ExportOrderName"/>
<asp:BoundField DataField="ExportPhone" HeaderText="ExportPhone" SortExpression="ExportPhone" />
<asp:BoundField DataField="ExportAddress" HeaderText="ExportAddress" SortExpression="ExportAddress" />
<asp:BoundField DataField="ExportAmount" HeaderText="ExportAmount" SortExpression="ExportAmount" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
</div>
<asp:Button ID="btnExportOrdersToPdf" runat="server"
             OnClick="btnExportOrdersToPdf_Click"
             Text="OrderDetails to PDF" />
</form>
</body>
</html>
Code behind:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class _ExportToPDF : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExportOrdersToPdf_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=OrderDetailsReport.pdf");
Response.Charset = string.Empty;
Response.ContentType = "application/vnd.pdf";
StringWriter swr = new StringWriter();
HtmlTextWriter hwr = new HtmlTextWriter(swr);
GvExportDetails.RenderControl(hwr);
StringReader srd = new StringReader(swr.ToString());
Document OrderDetialsReport = new Document(PageSize.A1, 12f, 12f, 12f, 10f);
HTMLWorker _htmlparser = new HTMLWorker(OrderDetialsReport);
PdfWriter.GetInstance(OrderDetialsReport, Response.OutputStream);
OrderDetialsReport.Open();
_htmlparser.Parse(srd);
OrderDetialsReport.Close();
Response.Write(OrderDetialsReport);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
  
}
Note:Errors might be come like "registerforeventvalidation can only be called during render"

Friday, May 3, 2013

RegisterForEventValidation can only be called during Render in asp.net

In previous post i have given how to Export Gridview data to excel in asp.net,Read data from excel file,Bind excel data to gridview,Export gridview data to word document.In this post i will explain how to resolve the RegisterForEventValidation error while generating reports from gridview.To resolve this kind of  error we need to render the controls then add the set the EnableEventValidation property to False at Page directive(you see the highlighted text).

Code behind:
This peace of code have to add in class.It will render the control to desired response while working with controls

public override void VerifyRenderingInServerForm(Control control)
{
}

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportToWord.aspx.cs" Inherits="ExportToWord" EnableEventValidation="false"
%>

Monday, April 16, 2012

globalization and localization in asp.net

Title: What is Globalization and Localization in asp.net

Description:
Providing content of your application in local is localisation.As assembly is targeted one particular assembly is satellite assembly.The extension of resource file .ress.Creating an .net application with the support for only one local language like Hindi is called as localisation and creating an application with the support of multiple languages is called as globalization

        For implementing localisation or globalization you need to create satellite assemblies.A satellite assembly is an assembly i:e targeted for one particular culture.For different cultures of different countries MS provides different key words.The keyword for US English culture is "EN-US",Keyword for Indian Hindi Culture ""HI-IN" and so on

  To create satellite assembly you need to create a resource file that has the extension .ress and this resource file will be compiled to a DLL file that is targeted for the culture for which resource file is created and this dll is called as satellite assembly

Wednesday, November 23, 2011

An object reference is required for the non-static field, method, or property

Title: An object reference is required for the non-static field

Description:
Each console application has it's own static main method. If we want to call a non static method in a static method it is not possible straight away because the static methods can be called without instantiate and non-static methods should need object instantiated before it get called. So, For this reason we have to create an object of class in main method to call the non-static methods. Now I will show you a simple example to call the non static method in static method.

A small example:
//Program Class
class Program
{ 
public int add(int n1,int n2)
{
int result=n1+n2;
return result;
} 
}

//Main method
static void main()
{
Program p=new Program();
p.add(n1,n2);
//logic 
}

Bel