我有这个工作代码,当选择了datagridview时显示另一种形式的图像
display_errors(false);
log_errors(true);
问题是当我选择没有图像的记录时,我收到此错误:
答案 0 :(得分:1)
Decalre myPhoto()在Try ... Catch块之外。此外,您应该在错误和非错误情况下关闭连接(最后或使用Using关键字)。
Dim myPhoto() as Byte = Nothing
With cmd
.Connection = cn
.CommandText = "SELECT studImage FROM studentInformation WHERE Studentid = " & dgv1.SelectedRows(0).Cells(0).Value
End With
Try
cn.Open()
myPhoto = CType(cmd.ExecuteScalar(), Byte())
Catch ex as Exception
Finally
If cn IsNot Nothing
cn.Close()
End If
End Try
return myPhoto
编辑:
Private Sub dgv1_DoubleClick(sender As Object, e As EventArgs) Handles dgv1.DoubleClick
frmEdit.Show()
Dim ms As MemoryStream
Dim photo as Byte() = changephoto(CInt(dgv1.SelectedCells(0).Value))
If photo IsNot Nothing
ms = new MemoryStream(photo)
frmEdit.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub