2016-03-20 129 views
0

我试图查看从datagridview到文本框和图片框的图像的所有信息。但是如果我的Isbn有破折号( - )或字母,那就错了。从字符串“”转换为“长”类型的转换无效。 VB.net

我的代码是这些

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) 

回答

0

该参数的LoadImage被定义为长。尝试使用字符串代替。

Private Sub Load_Image(ByVal iIMageID As String) 

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

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

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

+0

谢谢先生..它的工作原理:) –

+0

不客气。如果这有效,请接受我的答复为“已回答”。 – JerryM

相关问题