Hello Guys! Please help me out!!!
Curently finishing a small project, when my boss asked that the report should include the person Photo...
CR won't display photo unless it is stored directly into DataBase.
I read some where it is easuer to use Parameter to save both text and image.
Below is my code that is not helping me. It save text no problem but no image.
Private Sub btSaveNew_Click(sender As Object, e As EventArgs) Handles btSaveNew.Click
Dim conn As New SqlConnection
Dim daReg As New SqlDataAdapter
Dim dsReg As New DataSet
Dim cmdReg As New SqlCommand
Dim cmdReg1 As New SqlCommand
Dim check As Integer
'VERIFICA SE ESTA VAZIO
'If txtCode.Text = "" Or txtOwnerName.Text = "" Or txtOwnerBadge.Text = "" Or txtOwnerCompany.Text = "" Or txtOwnerSupervisor.Text = "" Or txtContacts.Text = "" Or cbDeviceType.Text = "" Or txtOtherDevType.Text = "" Or cbDeviceBrand.Text = "" Or txtOtherDevBrand.Text = "" Or txtDeviceModel.Text = "" Then
If txCode.Text = "" Or txOwner.Text = "" Or txBadge.Text = "" Or txCompany.Text = "" Or txSupervisor.Text = "" Or txContact.Text = "" Or cbDeviceType.Text = "" Or cbDeviceBrand.Text = "" Or txDeviceModel.Text = "" Or txSerialNumber.Text = "" Then
MsgBox("One or more fields are empty! Please, double check.")
Else
If MsgBox("Do you want to save the data with the code: " & txCode.Text & "?", MsgBoxStyle.OkCancel, "Input Confirmation") = MsgBoxResult.Cancel Then
'nao faz nada
Else
Dim RegDate As Date
RegDate = Format(dtPick.Value)
'Try
conn = Connect()
conn.Open()
cmdReg = conn.CreateCommand
'Foto-------------------------------------"""""""""""""""""""""""""""
Dim data As IDataObject
Dim bmap As Image
' Copy image to clipboard
SendMessage(hHwnd, CAP_EDIT_COPY, 0, 0)
' Get image from clipboard and convert it to a bitmap
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
PicCapture.Image = bmap
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = "Save an Image File"
saveFileDialog1.InitialDirectory = "\\MWKS485458\extra"
saveFileDialog1.FileName = txCode.Text
saveFileDialog1.ShowDialog()
' If the file name is not an empty string open it for saving.
If saveFileDialog1.FileName <> "" Then
' Saves the Image via a FileStream created by the OpenFile method.
Dim fs As System.IO.FileStream = CType _
(saveFileDialog1.OpenFile(), System.IO.FileStream)
PicCapture.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
lblPath.Text = saveFileDialog1.FileName
fs.Close()
'So textos---------------------------------------
cmdReg.CommandText = " INSERT INTO MAIN_REG (SECURITY_CODE,OWNER_NAME,OWNER_BADGE,OWNER_COMPANY,OWNER_SUPERVISOR,OWNER_CONTACTS,DEVICE_TYPE,DEVICE_TYPEII,DEVICE_BRAND,DEVICE_BRANDII,DEVICE_MODEL,DEVICE_SNUMBER,REGISTER_DATE,REGISTERED_BY,OWNER_PICTURE) VALUES ( '" & _
Trim(txCode.Text) & "','" & Trim(txOwner.Text) & "', '" & Trim(txBadge.Text) & "','" & Trim(txCompany.Text) & "','" & Trim(txSupervisor.Text) & "', '" & Trim(txContact.Text) & "', '" & Trim(cbDeviceType.Text) & _
"','" & Trim(txDeviceType.Text) & "', '" & Trim(cbDeviceBrand.Text) & "','" & Trim(txDeviceBrand.Text) & "','" & Trim(txDeviceModel.Text) & "', '" & Trim(txSerialNumber.Text) & "', '" & RegDate.ToString("yyy.MM.dd HH:MM") & "', '" & Trim(lblOwnerName.Text) & "', '" & Trim(lblPath.Text) & "')"
'---------------------------------------
'So textos---------------------------------------
'cmdReg1.CommandText = " UPDATE MAIN_REG SET SECURITY_CODE='" & Trim(txCode.Text) & "',OWNER_NAME='" & Trim(txOwner.Text) & "',OWNER_BADGE='" & Trim(txBadge.Text) & _
'"',OWNER_COMPANY='" & Trim(txCompany.Text) & "',OWNER_SUPERVISOR='" & Trim(txSupervisor.Text) & "',OWNER_CONTACTS='" & Trim(txContact.Text) & _
' "',DEVICE_TYPE='" & Trim(cbDeviceType.Text) & "',DEVICE_TYPEII='" & Trim(txDeviceType.Text) & "',DEVICE_BRAND='" & Trim(cbDeviceBrand.Text) & _
' "',DEVICE_BRANDII='" & Trim(txDeviceBrand.Text) & "',DEVICE_MODEL='" & Trim(txDeviceModel.Text) & "',DEVICE_SNUMBER='" & Trim(txSerialNumber.Text) & _
' "',REGISTER_DATE='" & RegDate.ToString("yyy.MM.dd HH:MM") & "',REGISTERED_BY='" & Trim(lblOwnerName.Text) & "',OWNER_PICTURE='" & Trim(lblPath.Text) & "'"
'---------------------------------------
'Create an Image object.
Using picture As Image = Image.FromFile(Trim(lblPath.Text))
'Create an empty stream in memory.
Using stream As New IO.MemoryStream
'Fill the stream with the binary data from the Image.
picture.Save(stream, Imaging.ImageFormat.Jpeg)
'Get an array of Bytes from the stream and assign to the parameter.
cmdReg1.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
End Using
End Using
cmdReg1.CommandText = " UPDATE MAIN_REG SET SECURITY_CODE='" & Trim(txCode.Text) & "',OWNER_NAME='" & Trim(txOwner.Text) & "',OWNER_BADGE='" & Trim(txBadge.Text) & _
"',OWNER_COMPANY='" & Trim(txCompany.Text) & "',OWNER_SUPERVISOR='" & Trim(txSupervisor.Text) & "',OWNER_CONTACTS='" & Trim(txContact.Text) & _
"',DEVICE_TYPE='" & Trim(cbDeviceType.Text) & "',DEVICE_TYPEII='" & Trim(txDeviceType.Text) & "',DEVICE_BRAND='" & Trim(cbDeviceBrand.Text) & _
"',DEVICE_BRANDII='" & Trim(txDeviceBrand.Text) & "',DEVICE_MODEL='" & Trim(txDeviceModel.Text) & "',DEVICE_SNUMBER='" & Trim(txSerialNumber.Text) & _
"',REGISTER_DATE='" & RegDate.ToString("yyy.MM.dd HH:MM") & "',REGISTERED_BY='" & Trim(lblOwnerName.Text) & "',OWNER_PICTURE='" & Trim(lblPath.Text) & "',photo=@Picture WHERE SECURITY_CODE='" & Trim(txCode.Text) & "'"
End If
End If
'-----------------------------------------------"""""""""""""""""""""""""""
check = cmdReg.ExecuteScalar.RecordsAffected
cmdReg1.ExecuteScalar()
If check > 0 Then
MsgBox(check & " Records have been Saved", MsgBoxStyle.Information, "Input Confirmation")
clean()
Else
MsgBox("An error occured while saving This information", MsgBoxStyle.Exclamation, "Input Confirmation")
End If
'Catch ex As Exception
'MsgBox(ex.Message, MsgBoxStyle.Critical, "Input Confirmation")
'End Try
End If
End If
conn.Close()
End Sub