Everest Helicopter Tour for a day

Book exciting packages at

VB.NET Application Crystal Report to PDF converter with ftp uploader

 Crystal Report to PDF converter with ftp uploader

This Application converts crystal report with mysql database in to pdf and upload pdf to ftp server running on linux.

1. Create the ODBC connection for mysql from control panel>admin tools> odbc manager. detail

2. in all of crystal report u should have the above connection in rpt files while creating them in crystal designer.

3. put all rpt files in some report folder. Do not have any direct reference to these rpt file in ur application. The application will scan the report folder and convert all rpt file in to pdf file and put them in pdf folder while uploading via ftp. It will also make a log.

4. open a new web application and use following code.

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

Imports System.IO

Imports System.Net

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


'FtpUploadFileToServer("", Application.StartupPath & "\" & "mou_summary" & ".pdf", "/home/admin", , "admin", "admin&123", , True)


'FtpUploadFileToServer("", Application.StartupPath & "\" & "WebMilesIssues" & ".pdf", "/home/admin", , "admin", "admin&123", , True)






End Sub

Private Function startjob() As Boolean

'Dim cr As New ReportDocument

Dim cr As ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument()

Dim di As New IO.DirectoryInfo(Application.StartupPath & "\reports")

Dim aryFi As IO.FileInfo() = di.GetFiles("*.rpt")

Dim fi As IO.FileInfo

For Each fi In aryFi


'' lblStatus.Text = lblStatus.Text & fi.Name & vbCrLf

lblStatus.Text = lblStatus.Text & " <-Converting " & fi.FullName

'Console.WriteLine("File Size (KB): {0}", strFileSize)

'Console.WriteLine("File Extension: {0}", fi.Extension)

'Console.WriteLine("Last Accessed: {0}", fi.LastAccessTime)

'Console.WriteLine("Read Only: {0}", (fi.Attributes.ReadOnly = True).ToString)

Dim thefile As String = fi.FullName ''Application.StartupPath & "\reports\CrystalReport2.rpt"


'CrystalReportViewer1.ReportSource = cr


''code for export

Dim x_opt As ExportOptions = cr.ExportOptions

x_opt.ExportFormatType = ExportFormatType.PortableDocFormat

x_opt.ExportDestinationType = ExportDestinationType.DiskFile

x_opt.DestinationOptions = New DiskFileDestinationOptions()

Dim hdd_opt As New DiskFileDestinationOptions()

CType(cr.ExportOptions.DestinationOptions, DiskFileDestinationOptions).DiskFileName = (Application.StartupPath & "\pdf\" & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf")


lblStatus.Text = lblStatus.Text & " Created " & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf "

'start upload

FtpUploadFileToServer("", Application.StartupPath & "\pdf\" & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf", "/home/admin", , "admin", "pwd", , True)

Catch e As Exception

lblStatus.Text = lblStatus.Text & " Error: " & e.Message & " for " & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf ->"

End Try


End Function

Private Function FtpUploadFileToServer(ByVal pServer As String, _

ByVal pUploadPathAndFileName As String, _

Optional ByVal pTargetPath As String = "", _

Optional ByVal pTargetFileName As String = "", _

Optional ByVal pUserName As String = "", _

Optional ByVal pPassword As String = "", _

Optional ByVal pPort As Integer = 21, _

Optional ByVal pUsePassive As Boolean = False) As Boolean

Dim objUploadStream As FileStream = Nothing

Dim objRequest As FtpWebRequest = Nothing

Dim objResponse As FtpWebResponse = Nothing

Dim objRequestStream As Stream = Nothing


lblStatus.Text = lblStatus.Text & " initiating upload " & pUploadPathAndFileName

objUploadStream = File.OpenRead(pUploadPathAndFileName)

Dim bytBuffer(CType(objUploadStream.Length, Integer)) As Byte

objUploadStream.Read(bytBuffer, 0, bytBuffer.Length)

If pTargetFileName.Length = 0 Then

pTargetFileName = IO.Path.GetFileName(objUploadStream.Name)

End If

Dim strUrl As String = String.Format("ftp://{0}:{1}/{2}/{3}", _

pServer, pPort, pTargetPath, pTargetFileName)

objRequest = CType(FtpWebRequest.Create(strUrl), FtpWebRequest)

If pUserName.Length > 0 And pPassword.Length > 0 Then

objRequest.Credentials = New NetworkCredential(pUserName, pPassword)

End If

objRequest.Method = WebRequestMethods.Ftp.UploadFile

objRequest.Proxy = Nothing

objRequest.KeepAlive = False

objRequest.UsePassive = pUsePassive

objRequestStream = objRequest.GetRequestStream()

objRequestStream.Write(bytBuffer, 0, bytBuffer.Length)


objResponse = CType(objRequest.GetResponse, FtpWebResponse)


lblStatus.Text = lblStatus.Text & " - " & objResponse.StatusDescription & " at " & Now.TimeOfDay.ToString & " ->"

Catch ex As Exception


lblStatus.Text = lblStatus.Text & ex.Message & "->"



If Not objRequestStream Is Nothing Then


End If

If Not objUploadStream Is Nothing Then



End If

If Not objRequest Is Nothing Then

objRequest = Nothing

End If

Catch ex As Exception


lblStatus.Text = lblStatus.Text & vbCrLf & ex.Message

End Try

End Try

End Function

Private Function makeLog() As Boolean

Dim path As String = "log.txt"

Dim sw As StreamWriter

' This text is added only once to the file.

If File.Exists(path) = False Then

' Create a file to write to.

sw = File.CreateText(path)



End If

' This text is always added, making the file longer over time

' if it is not deleted.

sw = File.AppendText(path)






'' Open the file to read from.

'Dim sr As StreamReader = File.OpenText(path)

'Dim s As String

'Do While sr.Peek() >= 0

' s = sr.ReadLine()

' Console.WriteLine(s)



End Function
- Vinod Kotiya


Asava Samuel said…
This has to be my favorite FTP Library for VB.NET:

Most Viewed Post

solution for facebook waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows

Google+ Followers