Click here to Skip to main content
14,244,275 members
Rate this:
Please Sign up or sign in to vote.
Hi,

I'm having a problem inserting a byte image into my datagridview;

Firstly Form Class:

Dim imgbyte As Byte() = Nothing


Form Load:

table.Columns.Add("Image", GetType(Byte()))


Button to generate data to datagridview;

table.Rows.Add(GetType(Byte)


Button2 to add image to picturebox1 as byte;

If OpenFileDialog1.ShowDialog = vbOK Then

        End If

        Dim myimage As Image = Image.FromFile(OpenFileDialog1.FileName)

        Dim imagestream As System.IO.MemoryStream = New System.IO.MemoryStream

        myimage.Save(imagestream, System.Drawing.Imaging.ImageFormat.Jpeg)
        imgbyte = imagestream.GetBuffer

        PictureBox1.Image = Drawing.Image.FromStream(imagestream)




I then get the following error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Type of value has a mismatch with column typeCouldn't store <System.Byte> in Image Column.  Expected type is Byte[].


Which flags the following line;

table.Rows.Add(GetType(Byte)


Anyone able to advise?

What I have tried:

Researching forums using search button.
Posted
Updated 20-Apr-17 11:06am
Comments
NotPolitcallyCorrect 20-Apr-17 15:52pm
   
So what is in imgbyte and why aren't you using it?
caf20012 20-Apr-17 16:04pm
   
What should i be using, as i'm fairly new to Visual Studios. Sorry for the incompetence.

I can't use picturebox1
ongilito 20-Apr-17 16:50pm
   
Where are you getting your image from?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing

Public Class Form1
    Dim ms As MemoryStream
    Dim imgData() As Byte
    Dim sqlSTR As String

    'This code saves the image in form of byte into SQl Server

    Sub saveImage()
        Dim p As New SqlParameter
        Try
          dim  sqlSTR As String= "UPDATE tableName SET imageField = @imageParameter"
            Using sqlCmmnd As New SqlClient.SqlCommand(sqlSTR, con)
                If Not PictureBox1.Image Is Nothing Then
                    ms = New MemoryStream
                    PictureBox1.Image.Save(ms, ImageFormat.Jpeg)
                    imgData = New Byte(ms.Length) {}
                    ms.Position = 0
                    ms.Read(imgData, 0, imgData.Length)
                    sqlCmmnd.Parameters.AddWithValue("@logo", imgData)
                Else
                    With p
                        .ParameterName = "@imageParameter"
                        .DbType = DbType.Binary
                        .Value = System.DBNull.Value
                    End With
                    sqlCmmnd.Parameters.Add(p)
                End If
                con.Open()
                sqlCmmnd.ExecuteNonQuery()
            End Using
            con.Close()           
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        End Try
    End Sub


   'This sub gets the byte image and converts it then displays on the picturebox, you can also add it to datagriedview

   Sub loadImage()
        Try
            Dim sqlDA As New SqlDataAdapter("SELECT imageField FROM tableName WHERE uniqueField ='" & Replace(TextBox1.Text.Trim, "'", "''") & "'",.con)
            Dim sqlCB As New SqlCommandBuilder(sqlDA)
            sqlDT.Reset()
            sqlDA.Fill(sqlDT)
            If Not IsDBNull(sqlDT.Rows(0)("imageField ")) Then
                imgData = sqlDT.Rows(0)("imageField ")
                Dim ms As MemoryStream = New MemoryStream(imgData)
                PictureBox1.Image = Image.FromStream(ms)
                ' for DataGridview DataGridView1.Rows(index).Add(Image.FromStream(ms))
            Else
                PictureBox1.Image = Nothing
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        End Try

    End Sub
End Class
   
Comments
caf20012 22-Apr-17 19:53pm
   
I'm getting the image from the following

 Dim opf As New OpenFileDialog
        opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"

        If opf.ShowDialog = DialogResult.OK Then

        End If
    End Sub
caf20012 22-Apr-17 19:58pm
   
Also i don't want to have a SQL server, as all the data is being inserted into the datagridview and then exported to Excel.

The SQl server option would be blocked in my case due to the computer having firewall restrictions
ongilito 3-May-17 5:35am
   
Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"
'add image variable
Dim myImage As Image=Nothing
If opf.ShowDialog = DialogResult.OK Then
'Get the image from openFileDialog to a variable
myImage=OpenFileDialog.Image
'now insert image to datagridview
DataGridView1.Rows(RowIndex).Add(myImage)
End If
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100