Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
Create Reports in VB.NET using WebBrowser Control - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home Visual Basic.NET Create Reports in VB.NET using WebBrowser Control

Create Reports in VB.NET using WebBrowser Control

E-mail
(1 vote, average: 5.00 out of 5)

Lots of reporting tools are available for .NET projects, such as crystal reports. However, you will need to install packages and dependencies to run reports on a local machine.
The following tutorial will show you how to generate a report in a VB.NET application using the web browser control.

First, create a new form, and name it as frmPrintPreview.
In this form, add an AxWebBrowser control, name it as wb. You can adjust the width, height of the web browser to fit in the form.

Switch to the View Code mode, and write the following function:



Function GenerateHTML () As String Dim dt As New DataTable
dt = LoadData
Dim i As Integer
Dim str As String

str += " <table width=100% border='1'>"
str += " <tr>"
str += "  <td><b>First Name</b></td>"
str += " <td><b>Last Name</b></td>"
str += " <td><b>Position</b></td>"
str += "</tr>"
For i = 0 To dt.Rows.Count - 1
str += "<tr>"
str += "  <td>" & dt.Rows(i).Item("FirstName") & "</td>"
str += " <td>" & dt.Rows(i).Item("LastName") & "</td>"
str += " <td>" & dt.Rows(i).Item("Position") & "</td>"
str += "</tr>"
Next
str += "</table>"
Return str
End Function


The above function generates an html document having a table in it.
Supposing that you already have a function that fill data into the dt DataTable (you can use DataSet or DataReader), a loop will create the dynamic part of the table and place the FirstName, LastName and Positions into cells.

Next, create a function as the following:


Function SaveTextFile(ByVal FilePath As String, ByVal FileContent As String, _
Optional ByVal Append As Boolean = False) As Boolean
Dim sw As System.IO.StreamWriter
Try
sw = New System.IO.StreamWriter(FilePath, Append)
sw.Write(FileContent)
Return True
Catch e As Exception
Return False
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Function


This function will save the HTML file onto your HardDrive using the StreamWriter class.
Then, the form load sub should look like this:


Private Sub frmPrintPreview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim tempFile As String = Path.ChangeExtension(Path.GetTempFileName(), "htm")
SaveTextFile(tempFile, GenerateHTML, False)
wb.Navigate(tempFile)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


It creates an tempFile, then save the content created by the GenerateHTML function into it, then show this tempFile using the WebBrowser control.

Debug your application and make sure there is no error. Based on this example and my Database, the report should look like this :

report-demo

You can add record counts, logos, header, css design to make this report looks more professional.

To print, simply right click on the content of the report and choose print.

Happy coding!