Take the 2-minute tour ×

I need to trigger a submit when saving my image file in SQL 2005 database.

A user will upload their image file.
My application will then check to see if the image file size is within specific requirements.
If so, then save the image into a table where the field is image data type. (if size does not meet requirements, display error message and exit sub).

The issue is if I save the image file to the database, the value is saved correctly (however, I have not checked the image size).

If I check the image file size first, THEN save it, the value saved is corrupted (incorrect).

Is there a way that I can check the file size, then save the file while doing a submit in code-behind?

My code is below:

            Dim iRequiredImageHeight As Integer = 80
            Dim iRequiredImageWidth As Integer = 280

            Dim imgBytes(FileUpload1.PostedFile.InputStream.Length) As Byte
            Dim stream As System.IO.Stream = FileUpload1.PostedFile.InputStream
            stream.Read(imgBytes, 0, imgBytes.Length)

            Dim imgData As System.Drawing.Image = System.Drawing.Image.FromStream(stream)
            Dim imgWidth As Integer = imgData.PhysicalDimension.Width
            Dim imgHeight As Integer = imgData.PhysicalDimension.Height

            If imgHeight > iRequiredImageHeight Or imgHeight > iRequiredImageWidth Then
                lblLogoMessage.Text = "The image you have uploaded is " & imgHeight & "px height by " & imgWidth & "px width. <br/> Required image size is " & iRequiredImageHeight & "px height by " & iRequiredImageWidth & "px width. <br/> Please resize the image and try again."
                lblLogoMessage.ForeColor = Drawing.Color.Red
                lblLogoMessage.Visible = True
                bLogoFit = False

                Dim LogoFile As String = FileUpload1.FileName.ToString.Trim
                Dim FileBytes(FileUpload1.PostedFile.InputStream.Length) As Byte

                FileUpload1.PostedFile.InputStream.Read(FileBytes, 0, FileBytes.Length)

                Dim SQL As String = "update CorpLogo "
                SQL += "set HeaderLogoName = @LogoFile, LogoImage = @LogoBytes "
                SQL += " where isonum='100'"

                Dim myConn As New SqlConnection(connString)
                Dim SQLcmd As New SqlCommand(SQL, myConn)
                SQLcmd.Parameters.AddWithValue("@LogoFile", LogoFile)
                SQLcmd.Parameters.AddWithValue("@LogoBytes", FileBytes)

            End If
share|improve this question
Why your value get corrupt? are you changing the image in any way? –  gbianchi Sep 28 '11 at 14:11
no, not changing the image in any way. –  DotNetRookie Sep 28 '11 at 14:13
ah.. you are reading the stream twice.. maybe that is the problem??? Never worked that way... –  gbianchi Sep 28 '11 at 14:16
yes, I am reading the stream twice. –  DotNetRookie Sep 28 '11 at 14:19
well, just read it one time!! what's the point on reading it twice? –  gbianchi Sep 28 '11 at 14:22

1 Answer 1

Just a brief note, since you have solved the problem. A Stream is like a firehose. It moves "water" from one location to another. Once you have pulled from the stream, the bits you are looking for are no longer there, as they are already pulled to a location rather than "held in the stream". In short, a stream is a bridge from one persistant location to another. Once items have cleared the stream, they are no longer there.

This is why you cannot get the bits (corruption) after you have gotten the bits.

share|improve this answer
wow... very cool analogy. Thanks very much –  DotNetRookie Sep 28 '11 at 14:56

Your Answer


