I want to print an image in Crystal Reports. I got some code from the web but the image doesn't show; only the text "ok" on the first column was shown.
Any suggestions would greatly appreciated.
Thanks in advance
Here is my code
Dim myRpt As New ImageReport
Dim txtHeader As CrystalDecisions.CrystalReports.Engine.TextObject = myRpt.Section2.ReportObjects("txtHeader")
txtHeader.Text = "IMAGE AND TEXT"
Dim txtDateNow As CrystalDecisions.CrystalReports.Engine.TextObject = myRpt.Section2.ReportObjects("txtDateNow")
txtDateNow.Text = Format(Now(), "MMMM d, yyyy")
Dim row As DataRow = Nothing
Dim DS As New DataSet
'ADD A TABLE TO THE DATASET
DS.Tables.Add("rp_recipe_cr_image_report")
'ADD THE COLUMNS TO THE TABLE
With DS.Tables(0).Columns
.Add("others_t1", Type.GetType("System.String"))
.Add("image", Type.GetType("System.Byte[]"))
End With
Dim fs As New FileStream("D:\asianporkroll.Jpg", FileMode.Open)
Dim br As New BinaryReader(fs)
Dim lBImageByte As Byte()
lBImageByte = New Byte(fs.Length + 1) {}
lBImageByte = br.ReadBytes(Convert.ToInt32(fs.Length))
row = DS.Tables(0).NewRow
row(0) = "ok"
row(1) = lBImageByte
DS.Tables(0).Rows.Add(row)
br.Close()
fs.Close()
myRpt.SetDataSource(DS)
CrystalReportViewer1.ReportSource = myRpt
CrystalReportViewer1.Refresh()
'DISPOSE OF THE DATASET
DS.Dispose()
DS = Nothing
Related
My datagridview doesn't have the time in datePurchased column. But when I exported it to PDF, it already includes the time. I need only the date not the time. Below is the sample code
//For exporting to pdf
Private Sub ExportButton_Click(sender As Object, e As EventArgs) Handles ExportButton.Click
connection.Close()
connection.Open()
Dim pdfTable As New PdfPTable(ReportDataGridView.ColumnCount)
pdfTable.DefaultCell.Padding = 1
pdfTable.WidthPercentage = 100
pdfTable.DefaultCell.HorizontalAlignment = Element. ALIGN_CENTER
Dim ptable As New Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
For Each column As DataGridViewColumn in ReportDataGridView.Columns
Dim cell as new PdfPCell(New Phrase(New Chunk(column.HeaderText, ptable)))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.FixedHeight = 30
pdfTable.AddCell(cell)
Next
For Each row as DataGridViewRow In ReportDataGridView.Rows
For each cell as DataGridViewCell In row.Cells
pdfTable.AddCell(cell.Value.ToString)
Next
Next
Dim folderpath As String = "C:\PDFs\"
If Not Directory.Exists(folderpath) Then
Directory.CreateDirectory(folderpath)
End If
Using sfd as New
SaveFileDialog()
sfd.ShowDialog()
sfd.OverWritePrompt = True
sfd.Title =" Save As"
sfd.AddExtension = True
sfd.DefaultExt = ".pdf"
Using stream As New FileStream(sfd.FileName & ".pdf",
FileMode.Create)
Dim pdfdoc As New
Document (PageSize.LETTER, 36.0F, 36.0F,36.0F,36.0F)
PdfWriter.GetInstance(pdfdoc.stream)
pdfdoc.Open()
pdfdoc.Add(pdfTable)
pdfdoc.Close()
stream.Close()
If File.Exists("path") Then
File.AppendAllText("path", "contents")
End If
pdfdoc.Close()
stream.Close()
End Using
End Using
End Sub
If you would ask me what's the data type of DatePurchased, it is date. I used date not datetime. Please help!
You would have to detect dates and explicitly format without time, e.g.
For each cell as DataGridViewCell In row.Cells
Dim cellValue = cell.Value
pdfTable.AddCell(If(TypeOf cellValue Is Date,
CDate(cellValue).ToShortDateString(),
cellValue.ToString()))
Next
So basically i generated picture boxes through codes to increment it whenever i ADD data in my database.. my problem is how can i get the PHOTOS from my database to be in my picture boxes.
Here is my code:
connection.Open()
cmd.Connection = connection
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT ID, Candidate_Name, Candidate_Fname,c_Photo from softeng.candidates"
da.SelectCommand = cmd
da.Fill(pdt)
For j As Integer = 0 To pdt.Rows.Count - 1
Dim a As String = pdt.Rows(j).Item(0)
Dim b As String = pdt.Rows(j).Item(1)
Dim c As String = pdt.Rows(j).Item(2)
Dim pb As New PictureBox
Dim lb As New Label
lb.Name = "lbid" & j
lb.Text = "Candidate ID:" & a & vbCrLf & b + c & vbCrLf
lb.AutoSize = True
lb.Size = New Point(100, 100)
pb.Name = "pb" & j
pb.Text = a
pb.AutoSize = True
pb.Size = New Point(100, 100)
pb.BorderStyle = BorderStyle.Fixed3D
FlowLayoutPanel1.Controls.Add(pb)
FlowLayoutPanel1.Controls.Add(lb)
Next
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
My codes for retrieving Pictures from my Database
Dim data As Byte() = DirectCast(dr("Photo"), Byte())
Dim ms As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(ms)
How can i put it to my code so i can retrieve photos when im looping my picture boxes?
Basically, you must make sure you are saving the images as binaries in database, then basically you read the binary information and convert it to an image object, showing in your control.
I'm pretty sure the link below has everything you need:
https://www.aspsnippets.com/Articles/Display-Binary-Image-from-Database-in-PictureBox-control-in-Windows-Application-using-C-and-VBNet.aspx
I am trying to display image in datagridview using ms-access and vb.net 2012.
my database in store image path does not image directly so when try to retrieve image in datagridview it shows the image path, not the image.
so how can I fix it?
Private Sub design_list_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Design_managementDataSet.design_details' table. You can move, or remove it, as needed.
'Me.Design_detailsTableAdapter.Fill(Me.Design_managementDataSet.design_details)
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Design Management\Database\design_management.accdb"
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
designDatagridShow()
End Sub
Private Sub designDatagridShow()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("select * from design_details", cnn)
da.Fill(dt)
design_datagridview.AutoGenerateColumns = False
Dim ColImage As New DataGridViewImageColumn
Dim Img As New DataGridViewImageCell
design_datagridview.ColumnCount = 3
design_datagridview.Columns(0).Name = "design_number"
design_datagridview.Columns(0).HeaderText = "Design Number"
design_datagridview.Columns(0).DataPropertyName = "design_number"
design_datagridview.Columns(1).Name = "design_collection"
design_datagridview.Columns(1).HeaderText = "Design Collection"
design_datagridview.Columns(1).DataPropertyName = "design_collection"
design_datagridview.Columns(2).Name = "price"
design_datagridview.Columns(2).HeaderText = "Design Price"
design_datagridview.Columns(2).DataPropertyName = "price"
design_datagridview.Columns(3).Name = "design_image"
design_datagridview.Columns(3).HeaderText = "Design Image"
design_datagridview.Columns(3).DataPropertyName = "design_image"
design_datagridview.DataSource = dt
Dim value As String
value = design_datagridview.Columns(3).HeaderText = "design_image"
Label1.Text = value
cnn.Close()
End Sub
Update 1:
Thanks.
Add another column that is of System.Drawing.Image datatype, then set the image based on your image column's path using System.Drawing.Image.FromFile(...) like the following:
Dim imageColumn As New DataColumn
imageColumn.ColumnName = "ActualImage"
imageColumn.DataType = GetType(System.Drawing.Image)
dt.Columns.Add(ImageColumn)
For Each row As DataRow in dt.Rows
row("ActualImage") = System.Drawing.Image.FromFile(row("design_image"))
Next
dt.AcceptChanges()
Dim dgvImageColumn As New DataGridViewImageColumn
dgvImageColumn.DataPropertyName = "ActualImage"
dgvImageColumn.Name = "ActualImage"
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
design_datagridview.Columns.Add(dgvImageColumn)
design_datagridview.DataSource = dt
Sub DataTable2CSV(ByVal table As DataTable, ByVal filename As String)
DataTable2CSV(table, filename, vbTab)
End Sub
Sub DataTable2CSV(ByVal table As DataTable, ByVal filename As String, _
ByVal sepChar As String)
Dim writer As System.IO.StreamWriter
Try
writer = New System.IO.StreamWriter(filename)
' first write a line with the columns name
Dim sep As String = ""
Dim builder As New System.Text.StringBuilder
For Each col As DataColumn In table.Columns
builder.Append(sep).Append(col.ColumnName)
sep = sepChar
Next
writer.WriteLine(builder.ToString())
' then write all the rows
For Each row As DataRow In table.Rows
sep = ""
builder = New System.Text.StringBuilder
For Each col As DataColumn In table.Columns
builder.Append(sep).Append(row(col.ColumnName))
sep = sepChar
Next
writer.WriteLine(builder.ToString())
Next
Finally
If Not writer Is Nothing Then writer.Close()
End Try
End Sub
I found this piece of coding on the internet after searching for "DataTable to File VB.net". What I would like to do is output this in filestream, is it possible to set up a response outputstream? Thanks
EDIT: To Clarify (Change up) Here is what I am working with now:
Protected Sub DoExportFinancials()
Dim ef As New ExportFinancials()
ef.Behavior = ExportFinancials.ObjectBehavior.Export
If ef.Load() = True Then
Me.GridView4.DataSource = ef.DT
Me.GridView4.DataBind()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
' Response.Charset = ""
'Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim stringWrite As StringWriter = New StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
GridView4.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End If
The below should work,
Dim FilePath As String = serverPath & "\App_Data\" & FileName
Response.Clear()
Response.ContentType = "text/csv"
Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName)
Response.TransmitFile(FilePath)
Response.End()
EDIT: Based on more details of Question,
[Change] : How to pass DataTable to Response Stream as CSV/Excel file, without writing it to Disk on server.
One way of accomplishing the above is first to Bind DataTable to a GridView and then the contents of GridView can be passed to the response stream using the below,
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim stringWrite As StringWriter = New StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
grid.RenderControl(htmlWrite) Response.Write(stringWrite.ToString())
Response.End()
i wrote program in vb.net. in my page, i have 3 textbox. in Txt_CardBarcode_TextChanged ,i wrote this codes:
Try
Dim stream_CardBarcode As System.IO.MemoryStream = New System.IO.MemoryStream
Dim cls As New Cls_Barcode
Dim pic_CardBarcode As System.Drawing.Image = Nothing
cls.btnEncode(pic_CardBarcode, Txt_CardBarcode.Text.Trim)
pic_CardBarcode.Save(stream_CardBarcode, System.Drawing.Imaging.ImageFormat.Png)
Dim f_cardBarcode As IO.FileStream = _
New IO.FileStream("C:\fafa.png", IO.FileMode.Create, IO.FileAccess.ReadWrite)
Dim b_cardBarcode As Byte() = stream_CardBarcode.ToArray
f_cardBarcode.Write(b_cardBarcode, 0, b_cardBarcode.Length)
f_cardBarcode.Close()
Dim ds As DS_Test
ds = New DS_Test
Dim Val_LabelBarcode() = {stream_CardBarcode.ToArray, Txt_ICCID.Text.Trim}
ds.Tables(2).Rows.Add(Val_LabelBarcode)
crp_CardBarcode.SetDataSource(ds.Tables(2))
Dim frm_CrpCardBarcode As New Frm_RepCardBarcode
frm_CrpCardBarcode.CrystalReportViewer1.ReportSource = crp_CardBarcode
GVSimInfo.DataSource = ds.Tables(2)
ds.Tables(2).Rows.Add(1)
GVSimInfo.Rows(GVSimInfo.Rows.Count - 1).Cells(0).Value = True
ds.Tables(2).Rows(0).Item(0) = True
ds.Tables(2).Rows(0).Item(1) = ""
ds.Tables(2).Rows(0).Item(2) = Txt_ICCID.Text
ds.Tables(2).Rows(0).Item(3) = ""
ds.Tables(2).Rows(0).Item(4) = ""
now, in run time, after filling 3textbox, new row add to gridview , but when user want to more than filling textboxes, new row in grid view replace on old row!!!
how to set new row add to grid view , instead of replace old row?
in my dataset, i put 3tables. tables(2) has 2 columns that save image barcode with byte array data type, but in my gridview ,i have 5 columns.
in run time give me error dialog,it is images from it:
If your DGV is not bound to any data Source:
GVSimInfo.Rows.Add(1);
If your DGV is bound to some Data Source then :
ds.Tables(2).Rows.Add(1)
Add this code after your last text box is filled and new row is needed.
to set the values you can use :
ds.Tables(2).Rows(0).Item("Column_number") = "your text"
Try
Dim stream_CardBarcode As System.IO.MemoryStream = New System.IO.MemoryStream
Dim cls As New Cls_Barcode
Dim pic_CardBarcode As System.Drawing.Image = Nothing
cls.btnEncode(pic_CardBarcode, Txt_CardBarcode.Text.Trim)
pic_CardBarcode.Save(stream_CardBarcode, System.Drawing.Imaging.ImageFormat.Png)
Dim f_cardBarcode As IO.FileStream = _
New IO.FileStream("C:\fafa.png", IO.FileMode.Create, IO.FileAccess.ReadWrite)
Dim b_cardBarcode As Byte() = stream_CardBarcode.ToArray
f_cardBarcode.Write(b_cardBarcode, 0, b_cardBarcode.Length)
f_cardBarcode.Close()
Dim ds As DS_Test
ds = New DS_Test
Dim Val_LabelBarcode() = {stream_CardBarcode.ToArray, Txt_ICCID.Text.Trim}
ds.Tables(2).Rows.Add(Val_LabelBarcode)
crp_CardBarcode.SetDataSource(ds.Tables(2))
Dim frm_CrpCardBarcode As New Frm_RepCardBarcode
frm_CrpCardBarcode.CrystalReportViewer1.ReportSource = crp_CardBarcode
ds.Tables(2).Rows.Add(1)
GVSimInfo.Rows(GVSimInfo.Rows.Count - 1).Cells(0).Value = True
ds.Tables(2).Rows(0).Item(0) = True
ds.Tables(2).Rows(0).Item(1) = ""
ds.Tables(2).Rows(0).Item(2) = Txt_ICCID.Text
ds.Tables(2).Rows(0).Item(3) = ""
ds.Tables(2).Rows(0).Item(4) = ""
GVSimInfo.DataSource = ds.Tables(2) <-------