Sunday, September 28, 2008

How to Display /Create PDF documents with ASP.NET

To display a pdf document with ASP.net here are some useful articles......

Visual Studio .NET comes with a useful version of Crystal Reports which allows you to create Crystal Reports (.rpt files) and then bind them to your database to produce PDF files on a web page, all from within the Visual Studio .NET GUI without ever even getting into the code. As easy as this is, you can have a lot more control over the process if you do it yourself.

This code shows you how to create and bind a DataSet and a Crystal Report file (.rpt) without even using Visual Studio at all (I used Web Matrix). It is assumed, however, that you have access to the Crystal Reports .dlls which came with Visual Studio .NET and that you have the ability to create .rpt files (I used Crystal Reports 9.0).

Details....

====================================================================

How to Display PDF documents with ASP.NET

This following article describes how to embed and display PDF documents in a webpage using simple ASP.NET custom server control. The method used, allows the developer to control the web page contents surrounding the embedded PDF.

This is not same as linking directly to a PDF which doesn't allow developer to control the appearance of the page.

There are two Visual Studio .NET solutions included with this download, one is web custom control library containing a single custom control used to render out the PDF, and the other is a test web site used to display a PDF through the use of the control.

Figure 3 (below) shows the solution explorer for the project. The project appearing at the top of the solution is the test web site, it contains only a single web page (default) and it includes a PDF file included for testing purposes. The bottom project is the web custom control library with the single control included (ShowPdf). The references in the test web site are per the default configuration; the custom control library references include the default references but also include System.Design namespace.

Details

Creating PDF documents in ASP.NET

First of all I create a simple "Hello PDF". Next I will create a more complex PDF document with tables. To start creating PDF documents you need to download the itextsharp library from http://sourceforge.net/projects/itextsharp/ and reference it in your project. The PDF documents are created "on the fly" by the web page "ShowPDF.aspx". This page does also a "Response.Redirect" to the created PDF. First import some required Namespaces:

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Next I decide in the Page_Load event which document was requested by the user: Details




Saturday, September 20, 2008

"Auto Refresh ASP.net page how to" by VB.net

Through this article, I am going to explain how we can auto refresh data on an ASP.NET
page after a certain interval. Lets for the time, use database Oracle and set our interval time for refreshing data as 10 seconds.

"
<% Me.Response.AppendHeader("Refresh", "10")
Call funt_onclick(Form_ID, System.EventArgs.Empty)
%>
"

The the page will be refreshed after every 10 seconds.

Now create two textboxes as named Textbox1,
Textbox2 under a form named form1.

In page1.aspx:
<%@ Page Language="VB" AutoEventWireup="True" CodeFile="page1.aspx.vb" Inherits="page1" %>
<% Me.Response.AppendHeader("Refresh", "10")
Call funt_onclick(form1, System.EventArgs.Empty)
%>



Code page (page1.aspx.vb) :
Partial Class page1
Inherits System.Web.UI.Page
Sub funt_onclick(ByVal sender As Object, ByVal e As EventArgs)

Call OraGET_Data()

End Sub

Private Sub OraGET_Data()
Dim OraConStr="" 'Give your Database connection string
Dim OraSql As String
Dim ORAcMD As System.Data.OracleClient.OracleCommand
Dim ORA_DR As System.Data.OracleClient.OracleDataReader

Dim myOraCon As New System.Data.OracleClient.OracleConnection(OraConStr)
Try

If (myOraCon.State = ConnectionState.Closed) Then
myOraCon.Open()
End If

'Oracle Database Sql Command
'+++++++++++++++++++++++++++
OraSql = "SELECT RINDEX,IDATE FROM DATA_table" ' VB.NET
ORAcMD = New System.Data.OracleClient.OracleCommand(OraSql, myOraCon)
ORAcMD.CommandType = CommandType.Text

'Oracle Table Reader
'+++++++++++++++++++++++++++
'dr_Stm = New System.Data.OracleClient.OracleDataReader
ORA_DR = ORAcMD.ExecuteReader()
ORA_DR.Read()

'initailize data for Textbox

Textbox1.Text = ORA_DR.Item("RINDEX")
Textbox2.Text = ORA_DR.Item("IDATE")

Catch ExOra As System.Data.OracleClient.OracleException
Response.Write(ExOra.ToString)
Catch Exp_nor As Exception
Response.Write(Exp_nor.ToString)

Finally
Call OraDBConCloz(myOraCon)

End Try

End Sub


Private Sub OraDBConCloz(ByVal OraCon As System.Data.OracleClient.OracleConnection)
'---- Check connection open or not -------------
If (OraCon.State = ConnectionState.Open) Then
OraCon.Close()
OraCon.Dispose()
End If
End Sub

End Class