tags:

views:

63

answers:

2

It's me your junior programmer extraodinaire

I'm trying to display an image from my database but when I try to extract the file it only contains 6 bytes of data which causes my program to throw an error. My instructor informed me that the file is too small for an image, leading me to believe that I'm not storing it properly. I would be most gracious if someone could identify any code that might cause this to happen.

this is my function grabbing/storing the file

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    Dim _image As Byte()
    _image = FileUpload1.FileBytes
    Dim conn As New SqlConnection
    Dim cmd As New SqlCommand

    Dim dr As SqlDataReader

    conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    cmd.Connection = conn
    cmd.CommandText = "Insert INTO mrg_Image(Image, UserId) VALUES('@image', @id)"

    cmd.Parameters.AddWithValue("@image", FileUpload1.FileBytes)
    cmd.Parameters.AddWithValue("@id", 4)


    conn.Open()
    dr = cmd.ExecuteReader
    conn.Close()

    'FileUpload1.SaveAs()

End Sub



    conn.Open()

file_bytes is the variable that contains 6 bytes as oppose to a thousand+ which an image should have

    Dim file_bytes As Byte() = cmd.ExecuteScalar()        
    Dim file_bytes_memory_stream As New System.IO.MemoryStream(file_bytes)
    Dim file_bytes_stream As System.IO.Stream = DirectCast(file_bytes_memory_stream, System.IO.Stream)

    Dim the_image As New System.Drawing.Bitmap(file_bytes_stream)

    context.Response.ContentType = "image/jpg"
    the_image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)        

    conn.Close()
End Sub
A: 

Try this

cmd.CommandText = "Insert INTO mrg_Image(Image, UserId) VALUES(@image, @id)"
cmd.Parameters.Add("@image", SqlDbType.Image)
cmd.Parameters("@image") = FileUpload1.FileBytes
cmd.Parameters.AddWithValue("@id", 4)

Edit Removed ";" forgot this was vb

Edit2 Changed [] to (). again forgot my VB

Conrad Frix
got several blue lines - 1st saying that sqldbType.Image is not declared and on the cmd.Paramaters["image"] = FileUpload1.FileBytes it says Property access must assign to the property or use it's values
PaulR
@user412318: Don't know why you're getting sqldbType.Image is not declared. Its been there since 1.1. I fixed the second one which easy
Conrad Frix
A: 

Try replacing '@image' with @image.

Also replace ExecuteReader with ExecuteNonQuery.


I wasn't aware of the FileBytes property, I've always done it so...

Dim _image(FileUpload1.PostedFile.InputStream.Length) As Byte FileUpload1.PostedFile.InputStream.Read(_image, 0, _image.Length)

Christopher Edwards
FileBytes was added in 2.0
Conrad Frix
I receive some errors when I try to convert to a executeNonQuery says, "Value of type, "integer" cannot be converted to system.data.sqlclient.sqlDatareader
PaulR
I just tried removing the single quotes and see what happens and I get a new error when I try to select the image from the db.error is: An invalid character was found in text content. Error processing resource 'http://localhost:1459/VS/images.ashx'. Wish I could put a break tag inside my ashx page.
PaulR
dr = cmd.ExecuteReader should be just cmd.ExecuteNonQuery, no need to have a dr at all.
Christopher Edwards
Also I think you also need... cmd.Parameters.Add("@image", SqlDbType.Image)cmd.Parameters["@image"] = FileUpload1.FileBytes from Conrad's answer...
Christopher Edwards