LOAD IMAGES DYNAMICALLY INTO CRYSTAL REPORTS USING VS2005

If you want to insert images from the file system (eg, C:\images folder) into crystal reports here is a simple process of doing that.

Add a dataset  to your project. Add a datatable to the dataset. Then add a row of data type system.byte().

dataset-photo

Add a new crystal report using the dataset created as its datasource. You are done now.

Add a new form to your project with a crystal report viewer and a button

The method below handles the button click event

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ofd As New OpenFileDialog()

        ofd.Multiselect = False

        ofd.Filter = “Images|*.jpg;*gif;*.bmp”

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

            Me.createReports(Me.savepic(ofd.FileName))

        Else

            MessageBox.Show(“Please select a Picture”)

        End If

    End Sub

 

Public Function savepic(ByVal fsource As String) As Byte()

        Dim arrImage() As Byte = Nothing

        If File.Exists(fsource) = False Then

            Return arrImage

        End If

        Dim img As Image = Image.FromFile(fsource)

        Dim ms As New IO.MemoryStream

        img.Save(ms, img.RawFormat)

        arrImage = ms.GetBuffer

        ms.Close()

        Return arrImage

    End Function

 

    Private Sub createReports(ByVal image() As Byte)

        Me.DataSet1.reports.Clear()

        Dim nrow As DataRow = DataSet1.reports.NewreportsRow

        nrow(0) = image

        DataSet1.reports.AddreportsRow(nrow)

        Dim rpt1 As New rptImages     ‘ crystal report object

        rpt1.SetDataSource(DataSet1)            ‘ set the datasource of crystalreport object, dataset 1 was the one created

        CrystalReportViewer1.ReportSource = rpt1

    End Sub

 

Sample picture when the application is run

Sample Picture of the Application
Sample Picture of the Application
Advertisements

DYNAMIC CONNECTION STRINGS IN VB.NET

To change the connection string of a typed dataset in vb.net 2005 and also Building a dynamic connection strings in Vb.net. Below are easy ways these can be done.

1. To change the connection string of a typed dataset, add the method below to the setting file. To see the setting.vb file, go the your project properties, on the settings tab, click on the the view code button and there you go.

Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

Dim db As New DBConnect

Me.Item("HospitalConnectionString")  =db.returnConnectionString

End Sub

Note: HospitalConnectionString is the name of the connections string as saved in your project settings

Below is the detail of the “returnConnectionString” procedure

Also note that servername, username, database, password are all saved in the settings

Public function returnConnectionString ()as string

Try

client = getClientName()

With My.Settings

If .servername = client Then

If Me.integratedSecurity = False Then

connectionString = "Data source= " & .servername & "; initial catalog= " & .database & ";user id= " & .username & " ;password = " & .password

Else

connectionString = "Data source= " & servername & "; initial catalog= " & database & "; Integrated Security= " & integratedSecurity

End If

Else

connectionString = "workstation id= " & client & " ;Data source= " & servername & " ;initial catalog= " & database & " ;user id= " & username & " ;password = " & password & " ;Integrated Security=" & integratedSecurity & " ; Connect TimeOut=30"

End If

End With

Catch ex As Exception

Msgbox(ex.ToString)

End Try

Return connectonString

End function

——————————

A function to return the name of the current machine(the machine the program is running on)

Public Function getClientName() As String

Dim client As String = ""

client = Dns.GetHostName

Return client

End Function

Saving items in the Settings

Below is code under the save setting buttons

With My.Settings

.Server = Me.txtServer.Text

.Dbase = Me.txtDbase.Text

.UserName = Me.txtUserName.Text

.Password = Me.txtPassWord.Text

.integrated = Me.ChkSecurity.Checked

.Save()

End With

Tags: , , , ,