Hi indradeo,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<hr />
<asp:Literal ID="ltEmbed" runat="server" />
<asp:Timer ID="Timer1" runat="server" OnTick="TimerTick" Interval="2000" />
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lblTime.Text = "Last Refreshed: " + DateTime.Now.ToString();
BindGridView();
}
}
protected void TimerTick(object sender, EventArgs e)
{
lblTime.Text = "Last Refreshed: " + DateTime.Now.ToString();
BindGridView();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void BindGridView()
{
GridView1.DataSource = GetData();
GridView1.AllowPaging = false;
GridView1.DataBind();
byte[] bytes;
using (MemoryStream memoryStream = new MemoryStream())
{
Document pdfDoc = new Document(PageSize.B6, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
TextReader sr = new StringReader(sw.ToString());
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
memoryStream.Close();
}
}
bytes = memoryStream.ToArray();
}
if (!Directory.Exists(Server.MapPath("~/File")))
{
Directory.CreateDirectory(Server.MapPath("~/File"));
}
File.WriteAllBytes(Path.Combine(Server.MapPath("~/File"), "Grid.pdf"), bytes);
string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"300px\" height=\"250px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}&download=1\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/File/Grid.pdf"));
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 10 * FROM Customers ORDER BY NEWID()";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblTime.Text = "Last Refreshed: " & DateTime.Now.ToString()
BindGridView()
End If
End Sub
Protected Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
lblTime.Text = "Last Refreshed: " & DateTime.Now.ToString()
BindGridView()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Private Sub BindGridView()
GridView1.DataSource = GetData()
GridView1.AllowPaging = False
GridView1.DataBind()
Dim bytes As Byte()
Using memoryStream As MemoryStream = New MemoryStream()
Dim pdfDoc As Document = New Document(PageSize.B6, 10.0F, 10.0F, 100.0F, 0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Dim sr As TextReader = New StringReader(sw.ToString())
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
memoryStream.Close()
End Using
End Using
bytes = memoryStream.ToArray()
End Using
If Not Directory.Exists(Server.MapPath("~/File")) Then
Directory.CreateDirectory(Server.MapPath("~/File"))
End If
File.WriteAllBytes(Path.Combine(Server.MapPath("~/File"), "Grid.pdf"), bytes)
Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""300px"" height=""250px"">"
embed += "If you are unable to view file, you can download from <a href = ""{0}&download=1"">here</a>"
embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
embed += "</object>"
ltEmbed.Text = String.Format(embed, ResolveUrl("~/File/Grid.pdf"))
End Sub
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 10 * FROM Customers ORDER BY NEWID()"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot