Retrieving image column from sql to datagridviewImage column

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Guys, Im 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.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() PrivateSub frmMainForm_FormClosing(ByVal sender AsObject, ByVal e As System.Windows.Forms.FormClosingEventArgs) HandlesMe.FormClosingCommand = 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").Valuecoworker_name is the coworker name column nameDim data AsByte() = CType(DataGridView1.Rows(x).Cells("CompanyStatusImage").Value, Byte()) CompanyStatusImage is image column name Dim ms As MemoryStream = New MemoryStream(data) m_barrImg = NewByte(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 typeCouldnt store in CompanyStatusImage Column. Expected type is Byte[].
I appreciate your help
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
.

View the full article
 
Back
Top