Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys, I'm going a little crazy with this,
In my datagrid I have 2 columns, names and images(image array). with the code below I was able to store this columns to an sql table with nvarchar and varbinary data type with help of a store procedure.
VB
 Private Sub frmMainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Command = New SqlCommand("insert_student", _Connection)
        _Command.CommandType = CommandType.StoredProcedure
        _Connection.Open()
        _Command.Parameters.Add(New SqlParameter("@_m_barrImg", SqlDbType.VarBinary))
        _Command.Parameters.Add(New SqlParameter("@_coworker_name", SqlDbType.NVarChar))

_Command.Parameters("@_coworker_name").Value = DataGridView1.Rows(x).Cells("coworker_name").Value
'coworker_name is the coworker name column name
Dim data As Byte() = CType(DataGridView1.Rows(x).Cells("CompanyStatusImage").Value, Byte())
' CompanyStatusImage is image column name
            Dim ms As MemoryStream = New MemoryStream(data)
            m_barrImg = New Byte(CInt(ms.Length - 1)) {}
            ms.Position = 0
            ms.Read(m_barrImg, 0, m_barrImg.Length)
            _Command.Parameters("@_m_barrImg").Value = m_barrImg
_Command.ExecuteNonQuery()


with this code I got coworker_name with all people names and under CompanyStatusImage I got Binary Data
Now, I tried to retrieve this columns with the code below, I got error
Type of value has a mismatch with column typeCouldn't store <system.drawing.bitmap> in CompanyStatusImage Column. Expected type is Byte[].


. any idea appreciated.
VB
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
        _Command = New SqlCommand("get_student", _Connection)
        _Command.CommandType = CommandType.StoredProcedure
        adapter = New SqlDataAdapter(_Command)
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        ds = New DataSet()
        adapter.Fill(ds, "tblImgData")
        m_barrImg = CType((ds.Tables(0).Rows(rno)("Companystatusimage")), Byte())
        Dim ms As New MemoryStream(m_barrImg)
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn With {.ColumnName = "coworker_name", .DataType = GetType(String)})
        dt.Columns.Add(New DataColumn With {.ColumnName = "CompanyStatusImage", .DataType = GetType(Byte())})
        
        Dim y As Integer
        Dim row As DataRow
        For Each r As DataRow In ds.Tables(0).Rows
            row = dt.NewRow
            row("coworker_name") = ds.Tables(0).Rows(y)(0)
'here is where I got error-->
            row("companyStatusImage") = Image.FromStream(ms)
            dt.Rows.Add(row)
            y = y + 1
        Next
        DataGridView1.DataSource = dt
Posted
Updated 24-Jun-13 16:54pm
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900