2014-09-20 102 views
0
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    GetPicture() 
End Sub 


Public Sub GetPicture() 

    con.Open() 

    Dim dt As New DataTable("Users") 
    Dim rs As New OleDb.OleDbDataAdapter("Select * from Users where StudentNumber='" & TextBox1.Text & "' ", con) 
    rs.Fill(dt) 
    DataGridView1.DataSource = dt 
    DataGridView1.Refresh() 
    Label1.Text = dt.Rows.Count 
    rs.Dispose() 
    con.Close() 

    If Val(Label1.Text) = 1 Then 
     Dim i As Integer 
     i = DataGridView1.CurrentRow.Index 
     PictureBox1.Image = FixNull(DataGridView1.Item(6, i).Value) 


    End If 
______________________________ 

我就行了這個錯誤:PictureBox1.Image = FixNull(DataGridView1.Item(6,I).value的)如何顯示/從Access數據庫獲取圖像到圖片框在VB

-> Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.


截圖:

回答

0

我只是figgured出來,這是解決方案


公共類形式

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\UsersDB.accdb") 
Dim cmd As New OleDbCommand("", con) 
Dim Reader As OleDb.OleDbDataReader 
Dim cn As New OleDbConnection 
Dim i As Integer 

公用Sub的GetData()

con.Open() 
    Dim dt As New DataTable("Users") 
    Dim rs As New OleDb.OleDbDataAdapter("Select * from Users where StudentNumber='" & TextBox1.Text & "' ", con) 
    rs.Fill(dt) 
    DataGridView1.DataSource = dt 
    DataGridView1.Refresh() 
    Label1.Text = dt.Rows.Count 
    rs.Dispose() 
    con.Close() 

    If Val(Label1.Text) = 1 Then 
     Dim i As Integer 
     i = DataGridView1.CurrentRow.Index 
     'Image 
     Dim bytes As [Byte]() = (DataGridView1.Item(6, i).Value) 
     Dim ms As New MemoryStream(bytes) 
     PictureBox1.Image = Image.FromStream(ms) 

    End If 

結束子

末級


相關問題