Solved

Value of type '1-dimensional array of Byte' cannot be converted to 'System.Drawing.Image'.

Posted on 2010-08-29
5
1,607 Views
Last Modified: 2013-11-25
Dear Experts,

I have a PictureBox i am trying to get bitmap picture from so as to save in MSSQL2005.

I guess i need to convert the picturebox image to Byte in order to save it into the Database.

I receive the above error messages from the point highlighted. Please HELP!!!


        CustEntity.CustPict = ReadImage(mImageFilePath)  ------>>Error

=================
 ReadImage(mImageFilePath)  = This has blue wringling underline.

=========
    Private mCustPict As Byte

    Public Property CustPict() As Byte
        Get
            Return mCustPict
        End Get
        Set(ByVal value As Byte)
            mCustPict = value
        End Set
    End Property

Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        'CustEntity.CustID = "1"
        CustEntity.CustName = TxtName.Text
        CustEntity.CustAddress = TxtAddress.Text
        CustEntity.CustDOB = TxtDOB.Text
        CustEntity.CustPict = ReadImage(mImageFilePath)  ------>>Error

        CustInfo.AddNewCustomer(CustEntity)
        ClearScreen()
    End Sub

    Private Shared Function ReadImage(ByVal p_postedImageFileName As String) As Byte()
        Dim p_fileType() As String = {".gif", ".jpg", ".bmp"}
        Dim isValidFileType As Boolean = False
        Try
            Dim file As New FileInfo(p_postedImageFileName)

            For Each strExtensionType As String In p_fileType
                If strExtensionType = file.Extension Then
                    isValidFileType = True
                    Exit For
                End If
            Next
            If isValidFileType Then
                Dim fs As New FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read)

                Dim br As New BinaryReader(fs)

                Dim image As Byte() = br.ReadBytes(CInt(fs.Length))

                br.Close()

                fs.Close()

                Return image
            End If
            Return Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Function


   Public Sub AddNewCustomer(ByVal CustInfo As CustomerEntities)
        Dim connection As SqlConnection = _
            New SqlConnection(CustConn.GetConnected())
        connection.Open()
        Try
            Dim command As SqlCommand = _
                New SqlCommand("AddCustomer", connection)
            command.CommandType = CommandType.StoredProcedure

            'command.Parameters.AddWithValue("@CustID", CustInfo.CustID)
            command.Parameters.AddWithValue("@CustName", CustInfo.CustName)
            command.Parameters.AddWithValue("@CustAddress", CustInfo.CustAddress)
            command.Parameters.AddWithValue("@CustDOB", CustInfo.CustDOB)
            command.Parameters.AddWithValue("@CustPix", CustInfo.CustPict)

            MsgBox("Rows inserted: " + _
                              command.ExecuteNonQuery().ToString)
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            connection.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:ukconcepts
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Marcjev earned 250 total points
ID: 33553345
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33553379
The byte data type stores an unsigned integer from 0 to 255, so it is no wonder that you were not able to use it to store an image :)
0
 

Author Comment

by:ukconcepts
ID: 33553419
i changed all Byte to [Byte]() and it worked fine. How do i award the marks?

Thanks both
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33553453
Marcjev is the one who really answered the question; s/he should get all the points :)
0
 

Author Closing Comment

by:ukconcepts
ID: 33553480
Thank you all. I quite agree too.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 49
VB.Net - Not Sure if Multi-threading is What I Need 4 31
VB.NET Textbox input validation 4 41
Parse this column 6 27
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question