Click here to Skip to main content
15,888,055 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys

I have saved some DataGridViewImage column to sql after converting them to Binary data along with some datagridtextcolumn data. I need to write them back to my datagridview control. here is part of the code and what my datatable look like. when I run this code I got error "invalid Parameter" any idea ? Thanks.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmdselect As SqlCommand = New SqlCommand("SELECT * FROM tblImgData", _Connection)
_Connection.Open()
Try
Using dataAdapter As New SqlDataAdapter(cmdselect)
Using commandBuilder As New SqlCommandBuilder(dataAdapter)
Dim dataTable As New DataTable()
dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture
dataAdapter.Fill(dataTable)
For x = 0 To (dataTable.DefaultView.Count - 1)
Dim barrImg As Byte() = DirectCast(dataTable.Rows(x).Item(1), Byte())
''''In this line I got the error
dataTable.Rows(x).Item(1) = Image.FromStream(New MemoryStream(barrImg))
Next
Dim bindingSource1 As New BindingSource()
bindingSource1.DataSource = dataTable
DataGridView1.DataSource = bindingSource1
End Using
End Using
Catch ex As Exception
MessageBox.Show("Error: " + ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
' finish the program
Application.[Exit]()
End Try
End Sub

DataTable:

name---- image------ message
Peter--- system.byte[]---- hello
dan---- system.byte[] ---- hello
Posted
Updated 18-Jun-13 16:44pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Jun-13 21:19pm    
In what line? Could you format the code with "pre" tag?
—SA

If you get an error that your data is invalid, then the error is probably in the code storing the image. Why are you storing images in your database, instead of the file system ?
 
Share this answer
 
Well, In my application I have a datagridview ctrl with 4 columns. 2 datagridviewtext columns and 2 datagridviewImage columns. with the code below along with the store procedure I was able to store all this columns into sql table. the reason behind storing in sql because the user will make changes to this columns I need to be able to save is just in case we need to reboot the PC. the images are embedded resource icons.
In sql table, I see all the names , messages and the images are in <binary data=""> format.
But with code above I can't retrieve this table to show it back on datagridview
VB
For X As Integer = 0 To DataGridView1.RowCount - 1
Dim strFn As String = DataGridView1.Rows(X).Cells"CompanyStatusImage".Value.ToString
               Dim fiImage As New FileInfo(strFn)
               Me.m_lImageFileLength = fiImage.Length
               m_barrImg = New Byte(Convert.ToInt32(Me.m_lImageFileLength) - 1) {}


VB
Private Sub frmMainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim _DelimitedString As String = String.Empty
        For X As Integer = 0 To DataGridView1.RowCount - 1

            _DelimitedString &= DataGridView1.Rows(X).Cells("Coworker_name").Value.ToString() & "," _
            & DataGridView1.Rows(X).Cells("StatusImage").Value.ToString() & "," _
            & DataGridView1.Rows(X).Cells("StatusImage1").Value.ToString() & "," _
            & DataGridView1.Rows(X).Cells("Message").Value.ToString() & "," _

VB
Next

       _Connection.Open()
       _Command = New System.Data.SqlClient.SqlCommand()
       _Command.CommandType = CommandType.StoredProcedure
       _Command.CommandText = "sp_InputWork"
       _Command.Connection = _Connection
       _Command.Parameters.AddWithValue("@_DelimitedString", System.Data.SqlDbType.NVarChar).Value = _DelimitedString
       _Command.Dispose()
       _Command.ExecuteNonQuery()

   End Sub


Store Procedure

SQL
USE [master]
GO
/****** Object:  StoredProcedure [dbo].[sp_InputWork]    Script Date: 06/17/2013 12:51:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON

go

ALTER PROCEDURE [dbo].[sp_SQLQuery3]
    @_DelimitedString nvarchar(MAX)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @_DataRow nvarchar(MAX)
    DECLARE @_coworker_name nvarchar(MAX)
        DECLARE @_TemporaryStorage nvarchar(MAX)
    DECLARE @_m_barrImg varchar(MAX)
DECLARE @_m_barrImg1 varchar(MAX)
DECLARE @_message nvarchar(MAX)
    DECLARE @_CurrentField int

    WHILE CHARINDEX(';', @_DelimitedString) > 0
    BEGIN
        SET @_DataRow = CAST(SUBSTRING(@_DelimitedString, 0, CHARINDEX(';', @_DelimitedString)) AS nvarchar(MAX))
        SET @_CurrentField = 1
        WHILE CHARINDEX(',', @_DataRow) > 0
            BEGIN
            SET @_TemporaryStorage = CAST(SUBSTRING(@_DataRow, 0, CHARINDEX(',', @_DataRow)) AS nvarchar(MAX))
IF @_CurrentField = 2
                SET @_m_barrImg = @_TemporaryStorage
IF @_CurrentField = 3
                SET @_m_barrImg1 = @_TemporaryStorage
 IF @_CurrentField = 1
            SET @_coworker_name = @_TemporaryStorage
 IF @_CurrentField = 4
                    SET @_company_name = @_TemporaryStorage

            SET @_DataRow = SUBSTRING(@_DataRow, CHARINDEX(',', @_DataRow) + 1, LEN(@_DataRow))
            SET @_CurrentField = @_CurrentField + 1
        END
       INSERT INTO tblImgData (coworker_name,CompanyStatusImage,CompanyStatusImage1,message) VALUES (@_coworker_name,@_m_barrImg,@_m_barrImg1,@_message)
	    SET @_DelimitedString = SUBSTRING(@_DelimitedString, CHARINDEX(';', @_DelimitedString) + 1, LEN(@_DelimitedString))
        END
END

sql table has 4 columns
nvarchar(max), image,image, nvarchar(max)
 
Share this answer
 
How would you save a datagridview with images to system ?
Thanks
dean
 
Share this answer
 
wow, I guess everybody is on vacation
 
Share this answer
 

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