Dynamic images in VB.NET 2005 Crystal Report - vb.net

I am using VB.NET 2005. I need to insert images to crystal report i have stored the path of images in my data base. there is no graphic location option in my version of Crystal report. how is it possible

If your data source is a datatable, add a new column of Byte() type like so:
Dim image As New DataColumn
With image
.ColumnName = "photo"
.DataType = GetType(Byte())
.AllowDBNull = True
End With
yourTable.Columns.Add(image)
Now set the value of your image based on the path stored in your data source. (You should include the column that stores the path of your image when you retrieve your data.)
For Each row As DataRow in yourTable.Rows
row("photo") = GetImageData(row("path_to_photo"))
Next
yourTable.AcceptChanges()
Private Function GetImageData(ByVal cFileName As String) As Byte()
Dim fs As System.IO.FileStream = _
New System.IO.FileStream(cFileName, _
System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs)
Return (br.ReadBytes(Convert.ToInt32(br.BaseStream.Length)))
End Function
Then set yourTable as the datasource of your report
yourReport.SetDataSource(yourTable)
Of course, your crystal report definition's data source must match the data source that you pass to it. Drag the "photo" column to your report and photos should be displayed.

Related

Writing an XML file to SQL database

I'm using Visual Studio 2019 with VB.NET. The goal is to dynamically write an xml file to a field in my SQL database. I've got various DataGridViews where the user can enter rows of information. The goal is to write this info to an XML file. I'm using an an XmlTextWriter to write the file to a MemoryStream since I don't want to actually write the file. I then try to Serialize the file and store it to an XML column in the database. The problem is my Serializer is returning nothing.
Here is the code:
Public Function CreateDgvXml(dgv As DataGridView) As String
Using ms As New IO.MemoryStream
Dim xtw As New Xml.XmlTextWriter(ms, Text.Encoding.UTF8)
Dim fn() As String = MapFieldNames(dgv)
With xtw
.WriteStartDocument(1)
.WriteStartElement("dgv")
.WriteAttributeString("name", dgv.Name)
For Each row As DataGridViewRow In dgv.Rows
.WriteStartElement("row")
For Each cell As DataGridViewCell In row.Cells
.WriteStartElement(fn(cell.ColumnIndex))
.WriteString(cell.FormattedValue.ToString)
.WriteEndElement()
Next
.WriteEndElement()
Next
.WriteEndElement()
.WriteEndDocument()
End With
Dim xs As New XmlSerializer(GetType(clsNewQuote))
xs.Serialize(ms, New clsNewQuote)
xtw.Flush()
Return xs.ToString
End Using
End Function
Private Function MapFieldNames(dgv As DataGridView) As String()
Dim result(dgv.ColumnCount - 1) As String
For Each col As DataGridViewColumn In dgv.Columns
result(col.Index) = col.HeaderText
Next
Return result
End Function
'''
Does anyone know how to do this properly?

External images not loading in RDLC report: Windows Forms Application VB.net

I'm working on a windows forms application, using vb.net and microsoft sql server as backend. As for the reports, I'm using microsoft's rdlc, which has been quite satisfactory for me until I was struck with this distinct problem.
So, in the report I'm using external images, which are loaded through local filepaths, passed as parameters. I retrieve these paths from the database and pass it to the report. This method worked for me for a long time, until it stopped working. Now the case is, that the external images are not loading in the report (a red cross is displayed instead of the image). As far as I feel, this problem has something to do with permissions on my PC because the same code and configurations worked for me before, but I'm not able to resolve it. I have searched everywhere to no end and would definitely want assistance.
My code for the report is:
Try
With Me.ReportViewer1.LocalReport
.DataSources.Clear()
.ReportPath = Application.StartupPath & "\RptArticle.rdlc"
.EnableExternalImages = True
Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics", imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath", "file://" & imagepathstring, True)
.SetParameters(imgparameter)
Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode", barpath)
Dim barcode As New ReportParameter("Barcode", "file://" & barpathstring, True)
.SetParameters(barcode)
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleAccessory", Accdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleSize", AccSdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleColour", AccCdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleColour", AccCdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticle", dt))
End With
Me.ReportViewer1.ZoomMode = ZoomMode.PageWidth
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Me.ReportViewer1.RefreshReport()
When I load the report, the output is:
Warning: Images with external URL references will not display if the report is published to a report server without an UnattendedExecutionAccount or the target image(s) are not enabled for anonymous access. (rsWarningFetchingExternalImages)
Warning: The value of the MIMEType property for the image ‘Image1’ is “application/octet-stream”, which is not a valid MIMEType. (rsInvalidMIMEType)
Warning: The value of the ImageData property for the image ‘Image1’ is “”, which is not a valid ImageData. (rsInvalidExternalImageProperty)
Warning: The value of the MIMEType property for the image ‘Image2’ is “application/octet-stream”, which is not a valid MIMEType. (rsInvalidMIMEType)
Warning: The value of the ImageData property for the image ‘Image2’ is “”, which is not a valid ImageData. (rsInvalidExternalImageProperty)
The thread 0x3948 has exited with code 0 (0x0).
Instead of using external images, you can pass your images as string to your report as parameters.
Convert your image to Base64 string first using this:
Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
Dim base64String As String = ""
Using ms As New System.IO.MemoryStream()
image.Save(ms, format)
Dim imageBytes As Byte() = ms.ToArray()
base64String = Convert.ToBase64String(imageBytes)
End Using
Return base64String
End Function
So this:
Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics", imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath", "file://" & imagepathstring, True)
.SetParameters(imgparameter)
Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode", barpath)
Dim barcode As New ReportParameter("Barcode", "file://" & barpathstring, True)
.SetParameters(barcode)
will be like this:
Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics", imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath", ImageToBase64(Image.FromFile(imagepathstring),<THE IMAGE FORMAT OF YOUR IMAGE>))
.SetParameters(imgparameter)
Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode", barpath)
Dim barcode As New ReportParameter("Barcode", ImageToBase64(Image.FromFile(barpathstring),<THE IMAGE FORMAT OF YOUR IMAGE>))
.SetParameters(barcode)
Then, in your report set the following for:
ImagePath
MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>
and in the Expression window:
=System.Convert.FromBase64String(Parameters!ImagePath.Value)
Barcode
MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>
=System.Convert.FromBase64String(Parameters!Barcode.Value)

RDLC generate barcode using ZXing

I am pretty new to the RDLC report feature, I am looking to generate labels from Product data within a SQL database. When the user opens this Product/Part they are greeted with the information. When the user then clicks a button this will open the Report which will pass the parameters across to the Report in order to generate the label.
Dim myparam As ReportParameter
Dim testParameter As New List(Of ReportParameter)
myparam = New ReportParameter("PartID", "Test")
testParameter.Add(myparam)
myparam = New ReportParameter("MRPID", "Test MRP")
testParameter.Add(myparam)
myparam = New ReportParameter("PartName", "Test Name")
testParameter.Add(myparam)
ReportViewer1.LocalReport.SetParameters(testParameter)
Dim writer As New BarcodeWriter
writer.Format = BarcodeFormat.CODE_128
PictureBox1.Image = writer.Write(MRPID)
Me.ReportViewer1.RefreshReport()
As you can see, I am using XLing to generate my barcodes, which I have been successful in making work with the 3 lines of code you see above. However, I have no idea how I can pass this or have this generate on the report when ran. The barcode will be generated from the MRPID ie(TV001232). I understand this part is wrong "writer.Write(MRPID)" but I replaced the parameter value with MRPID so you could understand what I am trying to achieve.
Convert your image to Base64 string first using this:
Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
Dim base64String As String = ""
Using ms As New System.IO.MemoryStream()
image.Save(ms, format)
Dim imageBytes As Byte() = ms.ToArray()
base64String = Convert.ToBase64String(imageBytes)
End Using
Return base64String
End Function
So this:
myparam = New ReportParameter("MRPID", "Test MRP")
testParameter.Add(myparam)
Should be like this:
Dim writer As New BarcodeWriter
writer.Format = BarcodeFormat.CODE_128
myparam = New ReportParameter("MRPID", ImageToBase64(writer.Write(MRPID),<THE IMAGE FORMAT OF YOUR IMAGE>))
testParameter.Add(myparam)
Then, in your report set the following:
MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>
and in the Expression window:
=System.Convert.FromBase64String(Parameters!MRPID.Value)

How to import csv file in Datagridview with certain columns and rows?

I'm new to VB.net and I don't know how to display certain columns and rows in datagridview that was imported from CSV file. My problem is I have many columns and all I want to display is 2 columns:
Name,Age,Mobile Number,ID number
Alex,18,09848484841,0010
George,19,02987654321,0020
Toni,17,09277470257,0030
How can I display only the Name & Age columns and its rows?
If you use a datatable you get the data structure and collection together. something like this:
Dim sr As New IO.StreamReader(filename)
Dim dt As New DataTable
Dim newline() As String = sr.ReadLine.Split(","c)
dt.Columns.AddRange({New DataColumn(newline(0)), _
New DataColumn(newline(1))})
While (Not sr.EndOfStream)
newline = sr.ReadLine.Split(","c)
Dim newrow As DataRow = dt.NewRow
newrow.ItemArray = {newline(0), newline(1)}
dt.Rows.Add(newrow)
End While
DataGridView1.DataSource = dt
Use a custom class with properties that match the data you want to store and make an instance of that class for each row of data use read, then have a List(Of {custom class}) to hold each object and the DGV's DataSource property can view the collection in the grid. The property names in the class will be used as the header.

Put content of fileStream in dataset

I want to read Stream that i get from XtrapivotGrid of DevExpress. I can save it in the computer but what i want is to save it in one of my table in my dataset called dataset1.
For now i have that code who permit to save it the directory Temp:.
Using FS As New IO.FileStream("D:\Temp\qqc.layout", IO.FileMode.Create)
PivotGridControl1.SaveLayoutToStream(FS)
End Using
Dim read As New System.IO.FileStream("D:\Temp\qqc.layout", IO.FileMode.Open, IO.FileAccess.Read)
DataSet1.LayoutMainRapport.ReadXml(read)
DataSet1.AcceptChanges()
read.Close()
The table LayoutMainRapport have 3 columns:
ID(Int)
Name(nvarchar(50))
Content(xml).
The output from the stream is xml.
thanks in advance!
I believe you should convert your saved layout into string and then save this string to database (and of course you can load this layout back from the database string value):
Private Function SaveLayoutToString(ByVal dxControl As DevExpressControl) As String
Using ms As MemoryStream = New MemoryStream()
dxControl.SaveLayoutToStream(ms)
Return Convert.ToBase64String(ms.ToArray())
End Using
End Function
Private Sub RestoreLayoutFromString(ByVal dxControl As DevExpressControl, ByVal layout As String)
If String.IsNullOrEmpty(layout) Then
Return
End If
Using ms As MemoryStream = New MemoryStream(Convert.FromBase64String(layout))
dxControl.RestoreLayoutFromStream(ms)
End Using
End Sub
Here DevExpressControl dxControl is the DevExpress control which supports saving and loading layout (XtraPivotGrid, XtraGrid, XtraLayoutControl etc.)
I simply had to save a name for the xml and after save it into my dataset.
Dim saveDialog As SaveLayout = New SaveLayout
If saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim read As New IO.MemoryStream
PivotGridControl1.SaveLayoutToStream(read)
read.Position = 0
Dim lecteur As New IO.StreamReader(read)
Dim ligne As DataRow = DataSet1.LayoutMainRapport.NewRow()
ligne("Nom") = saveDialog.txtNom.Text
ligne("Contenu") = lecteur.ReadToEnd()
DataSet1.LayoutMainRapport.Rows.Add(ligne)
LayoutMainRapportTableAdapter.Update(DataSet1)
End If