A
Adam
I am having difficulty retrieving images from a SQL database.
Here is the code I use to UPLOAD the image to the database:
<form id="Form1" method="post" enctype="multipart/form-data"
runat="server">
<INPUT id="UploadedImageFile" runat=server type="file" size="86">
</form>
Codebehind
----------
Private Sub InsertIntoDB()
'then get the stream
Dim MyStream As System.IO.Stream
MyStream = UploadedImageFile.PostedFile.InputStream
'MyStream = upImage.InputStream
'Dim MyData(MyStream.Length) As Byte
Dim MyData(UploadedImageFile.PostedFile.InputStream.Length) As
Byte
Dim n = MyStream.Read(MyData, 0,
UploadedImageFile.PostedFile.InputStream.Length)
Dim CultivarImagesRow As dsCultivarVision.CultivarImagesRow =
ds.CultivarImages.NewCultivarImagesRow
With CultivarImagesRow
.CultivarId = lbCultivarName.SelectedValue
.ImageClassificationId =
ddImageClassification.SelectedValue
.ImageUseId = ddImageUse.SelectedValue
.ImageFormatId = ddImageFormat.SelectedValue
.ImageDescription = txtImageDescription.Text
.Credit = txtImageCredit.Text
.ImageBinary = MyData
Dim conn As New
SqlConnection(Application("ConnectionString"))
conn.Open()
Dim connInsert As New SqlCommand _
("Insert INTO XCultivarImages (.CultivarID,
..ImageClassificationID, .ImageUseId, .ImageFormatID,
..ImageDescription, .Credit, .ImageBinary) values(@CultivarID,
@ImageClassificationID, @ImageUseId,
@ImageFormatID, @ImageDescription, @Credit, @ImageBinary)", conn)
connInsert.Parameters.Add(New SqlParameter("@CultivarID",
SqlDbType.Int, 4, "CultivarID"))
connInsert.Parameters.Add(New
SqlParameter("@ImageClassificationID", SqlDbType.Int, 4,
"ImageClassificationID"))
connInsert.Parameters.Add(New SqlParameter("@ImageUseID",
SqlDbType.Int, 4, "ImageUseID"))
connInsert.Parameters.Add(New
SqlParameter("@ImageFormatID", SqlDbType.Int, 4, "ImageFormatID"))
connInsert.Parameters.Add(New
SqlParameter("@ImageDescription", SqlDbType.Text, 16,
"ImageDescription"))
connInsert.Parameters.Add(New SqlParameter("@Credit",
SqlDbType.VarChar, 65, "Credit"))
connInsert.Parameters.Add(New SqlParameter("@ImageBinary",
SqlDbType.Image, 16, "ImageBinary"))
connInsert.Parameters("@CultivarId").Value = .CultivarId
connInsert.Parameters("@ImageClassificationID").Value =
..ImageClassificationId
connInsert.Parameters("@ImageUseID").Value = .ImageUseId
connInsert.Parameters("@ImageFormatID").Value =
..ImageFormatId
connInsert.Parameters("@ImageDescription").Value =
..ImageDescription
connInsert.Parameters("@Credit").Value = .Credit
connInsert.Parameters("@ImageBinary").Value = .ImageBinary
connInsert.ExecuteNonQuery()
conn.Close()
End With
End Sub
On my aspx that should display the image I have the following tag:
<asp:Image ID="imgCultivar"
ImageUrl="displayimage.aspx?cultivarid=16775" width=200 height=200
Runat="server"></asp:Image>
On displayimage.aspx.vb I have the following code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim intcultivarid = Request.QueryString("cultivarid")
Dim sqltext = "SELECT imagebinary from xcultivarimages where
cultivarid = " & intcultivarid
Dim conn As New SqlConnection(Application("ConnectionString"))
Dim command As New SqlCommand(sqltext, conn)
conn.Open()
Dim dr As SqlDataReader = command.ExecuteReader
Do While (dr.Read())
If Not dr(0) Is System.DBNull.Value Then
Response.ContentType = "Image/jpeg"
Response.BinaryWrite(dr(0))
Response.Flush()
End If
Loop
conn.Close()
End Sub
WHen I upload jpgs, and try and display them, I get the dreaded
graphic with the red "x" through it signifying no image available.
Please please please help me as I am at wit's end after reading other
people's posts and web pages.
Adam
Here is the code I use to UPLOAD the image to the database:
<form id="Form1" method="post" enctype="multipart/form-data"
runat="server">
<INPUT id="UploadedImageFile" runat=server type="file" size="86">
</form>
Codebehind
----------
Private Sub InsertIntoDB()
'then get the stream
Dim MyStream As System.IO.Stream
MyStream = UploadedImageFile.PostedFile.InputStream
'MyStream = upImage.InputStream
'Dim MyData(MyStream.Length) As Byte
Dim MyData(UploadedImageFile.PostedFile.InputStream.Length) As
Byte
Dim n = MyStream.Read(MyData, 0,
UploadedImageFile.PostedFile.InputStream.Length)
Dim CultivarImagesRow As dsCultivarVision.CultivarImagesRow =
ds.CultivarImages.NewCultivarImagesRow
With CultivarImagesRow
.CultivarId = lbCultivarName.SelectedValue
.ImageClassificationId =
ddImageClassification.SelectedValue
.ImageUseId = ddImageUse.SelectedValue
.ImageFormatId = ddImageFormat.SelectedValue
.ImageDescription = txtImageDescription.Text
.Credit = txtImageCredit.Text
.ImageBinary = MyData
Dim conn As New
SqlConnection(Application("ConnectionString"))
conn.Open()
Dim connInsert As New SqlCommand _
("Insert INTO XCultivarImages (.CultivarID,
..ImageClassificationID, .ImageUseId, .ImageFormatID,
..ImageDescription, .Credit, .ImageBinary) values(@CultivarID,
@ImageClassificationID, @ImageUseId,
@ImageFormatID, @ImageDescription, @Credit, @ImageBinary)", conn)
connInsert.Parameters.Add(New SqlParameter("@CultivarID",
SqlDbType.Int, 4, "CultivarID"))
connInsert.Parameters.Add(New
SqlParameter("@ImageClassificationID", SqlDbType.Int, 4,
"ImageClassificationID"))
connInsert.Parameters.Add(New SqlParameter("@ImageUseID",
SqlDbType.Int, 4, "ImageUseID"))
connInsert.Parameters.Add(New
SqlParameter("@ImageFormatID", SqlDbType.Int, 4, "ImageFormatID"))
connInsert.Parameters.Add(New
SqlParameter("@ImageDescription", SqlDbType.Text, 16,
"ImageDescription"))
connInsert.Parameters.Add(New SqlParameter("@Credit",
SqlDbType.VarChar, 65, "Credit"))
connInsert.Parameters.Add(New SqlParameter("@ImageBinary",
SqlDbType.Image, 16, "ImageBinary"))
connInsert.Parameters("@CultivarId").Value = .CultivarId
connInsert.Parameters("@ImageClassificationID").Value =
..ImageClassificationId
connInsert.Parameters("@ImageUseID").Value = .ImageUseId
connInsert.Parameters("@ImageFormatID").Value =
..ImageFormatId
connInsert.Parameters("@ImageDescription").Value =
..ImageDescription
connInsert.Parameters("@Credit").Value = .Credit
connInsert.Parameters("@ImageBinary").Value = .ImageBinary
connInsert.ExecuteNonQuery()
conn.Close()
End With
End Sub
On my aspx that should display the image I have the following tag:
<asp:Image ID="imgCultivar"
ImageUrl="displayimage.aspx?cultivarid=16775" width=200 height=200
Runat="server"></asp:Image>
On displayimage.aspx.vb I have the following code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim intcultivarid = Request.QueryString("cultivarid")
Dim sqltext = "SELECT imagebinary from xcultivarimages where
cultivarid = " & intcultivarid
Dim conn As New SqlConnection(Application("ConnectionString"))
Dim command As New SqlCommand(sqltext, conn)
conn.Open()
Dim dr As SqlDataReader = command.ExecuteReader
Do While (dr.Read())
If Not dr(0) Is System.DBNull.Value Then
Response.ContentType = "Image/jpeg"
Response.BinaryWrite(dr(0))
Response.Flush()
End If
Loop
conn.Close()
End Sub
WHen I upload jpgs, and try and display them, I get the dreaded
graphic with the red "x" through it signifying no image available.
Please please please help me as I am at wit's end after reading other
people's posts and web pages.
Adam