'*** for Logo Sub Report (Start) ***'
Dim dtLogo As New DataTable("logo")
Dim dr As DataRow
dtLogo.Columns.Add(New DataColumn("Logo", GetType(System.Byte())))
dtLogo.Columns.Add(New DataColumn("Company", GetType(String)))
Dim fiStream As New FileStream(Server.MapPath("Images/thaicreate.jpg"), FileMode.Open)
Dim binReader As New BinaryReader(fiStream)
Dim pic() As Byte = {}
pic = binReader.ReadBytes(fiStream.Length)
dr = dtLogo.NewRow
dr("Logo") = pic
dr("Company") = "ThaiCreate.Com Co,. LTD"
dtLogo.Rows.Add(dr)
fiStream.Close()
binReader.Close()
'*** for Logo Sub Report (End) ***'
สร้าง Sub Report (Subreport) บน Crystal Report แบบ Step by Step (VB.NET /C#)
กรณีนี้ ภาพจะเปลี่ยนตามข้อมูลที่เลือกไหมคะ ขอสอบถามเพิ่มเติมค่ะ พอดีเขียนเป็นแบบ Window form ธรรมดา แต่ใน Codeตัวอย่างเป็น ASP ใช้ได้เหมือนกันใช่ไหมคะ พอดีเขียนแล้วติดตรง Inherits System.Web.UI.Page
และติดตรง Dim fiStream As New FileStream(Server.MapPath(dt.Rows(i)("Picture")), FileMode.Open)
ใน Code error ตรงคำว่า Server จะแก้อย่างไรคะ
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim DSreport As New DataSet()
Dim SqlReader As SqlClient.SqlDataReader
Dim Conn As New SqlClient.SqlConnection()
Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString, strSQL As String
strConnString = "Server=192.168.0.42;Database=DBMT;user id=sa;password=P@ssw0rd"
' strSQL = "SELECT p.pro_code,p.pro_name,p.pro_cost,p.pro_min,p.pro_stock,u.u_unitname FROM MTcodeproducts p left join MTunit u on p.pro_unit=u.u_unitid where p.group_code='804' and p.pro_min < p.pro_stock "
strSQL = "SELECT * FROM MTmach WHERE pro_mach = '" & Me.TextBox1.Text & "' "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds, "myDataTableHM")
dt = ds.Tables(0)
Conn = New SqlConnection(strConnString)
Conn.Open()
Dim DTT As New DataTable("myDataTableHM")
' DTT.Columns.Add("ID", GetType(String))
' DTT.Columns.Add("FName", GetType(String))
' DTT.Columns.Add("LName", GetType(String))
DTT.Columns.Add("mach_img", GetType(System.Byte()))
DSreport.Tables.Add(DTT)
Dim SqlAdapter As New SqlClient.SqlDataAdapter(strSQL, Conn)
SqlReader = SqlAdapter.SelectCommand.ExecuteReader
If SqlReader.HasRows Then
While SqlReader.Read
Dim path As String = Application.StartupPath + "\" + SqlReader.Item("mach_img").ToString()
Dim FilStr As New FileStream(path, FileMode.Open) ' เปิด file แบบ Stream เพื่ออ่านเป็น Binary
Dim BinRead As New BinaryReader(FilStr)
Dim DR As DataRow = DSreport.Tables("myDataTableHM").NewRow
' Add ข้อมูลที่อ่านจาก SQL Base ใส่เข้าไปแต่ละ Rows ของ Temp Table
' DR.Item("ID") = SqlReader.Item("ID")
' DR.Item("FName") = SqlReader.Item("FName")
' DR.Item("LName") = SqlReader.Item("LName")
' Column Photo ใส่ข้อมูล Binary
DR.Item("mach_img") = BinRead.ReadBytes(BinRead.BaseStream.Length)
DSreport.Tables("myDataTableHM").Rows.Add(DR) 'Add Row เข้าไปใน Temp Table
FilStr.Close() '//ปิด FileStream
BinRead.Close()
End While
SqlReader.Close()
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim rpt As New ReportDocument()
Dim directory As String = My.Application.Info.DirectoryPath
'rpt.Load(directory & "\myCrystalReport1.rpt")
rpt.Load("D:\Program_ECF\MT\ECF_LOTUS\CrystalReportHmach.rpt")
rpt.SetDataSource(dt)
Me.CrystalReportViewer1.ReportSource = rpt
Me.CrystalReportViewer1.Refresh()
End If
End Sub