从字符串“”到“Long”类型的转换无效。 VB.net

时间:2016-03-20 08:14:17

标签: vb.net image datagridview

我尝试使用datagridview中的图像查看所有信息到文本框和图片框。但如果我的Isbn有短划线( - )或字母,那就有错误。

我的代码就是这些

  Private Sub Load_Image(ByVal iIMageID As Long)
    Dim sConnString As String

    Dim arrImage() As Byte
    Dim myMS As New IO.MemoryStream

    Dim reader As MySqlDataReader


    Try

        Call ConnectDatabase()


        Dim query As String
        query = "select ISBN, Subject, BookName, Publisher, Author, PublishingYear, Status, image from Books where ISBN= " & iIMageID
        Command = New MySqlCommand(query, conn)
        reader = Command.ExecuteReader

        If reader.HasRows Then
            While reader.Read

                Label2.Text = reader("ISBN")
                Label3.Text = reader("Subject")
                Label1.Text = reader("BookName")
                Label4.Text = reader("Publisher")
                Label5.Text = reader("Author")
                Label6.Text = reader("PublishingYear")
                Label7.Text = reader("Status")



                arrImage = reader("image")
                For Each ar As Byte In arrImage
                    myMS.WriteByte(ar)
                Next
                '
                Me.PictureBox1.Image = System.Drawing.Image.FromStream(myMS)
            End While
        End If

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

然后在CellcontentClick事件

 Load_Image(Me.MetroGrid1.Item(0, Me.MetroGrid1.CurrentRow.Index).Value.ToString)

1 个答案:

答案 0 :(得分:0)

LoadImage参数定义为Long。请尝试使用字符串。

Private Sub Load_Image(ByVal iIMageID As String)

您还必须在SQL调用中添加单引号。

    query = "select ISBN, Subject, BookName, Publisher, Author, PublishingYear, Status, image from Books where ISBN= '" & iIMageID & "'"

为了安全起见,请考虑创建一个存储过程并调用它。