Image download upload display problems on aspx (code pasted in)

  • Thread starter Thread starter Adam
  • Start date Start date
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
 
Hi,
Couldnt think of much except you havent specified an explicit cast on
datareader dr(0)

I am doing something similar and this might help you

Code to write it to a aspx page
public class DisplayImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if(Request["ImageID"] != null)
{
int ImgID = int.Parse(Request["ImageID"]);
CodePlayDB myCode = new CodePlayDB();
string ContentType = "";
byte[] Image = myCode.GetImageFromDB(ImgID, ref ContentType);
Response.ContentType = ContentType;
Response.BinaryWrite(Image);
}
}
}

Code to retrieve it from db
public byte[] GetImageFromDB(int myImageID, ref string myContentType)
{
SqlConnection myCon = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
try
{
SqlCommand myCommand = new SqlCommand("sp_Images_Select", myCon);
myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parameterImageID = new SqlParameter("@ImageID",
SqlDbType.Int, 4);
parameterImageID.Value = myImageID;
myCommand.Parameters.Add(parameterImageID);

myCon.Open();
SqlDataReader myReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
byte[] Img = null;
if(myReader.Read())
{
myContentType = myReader["ImageContentType"].ToString();
Img = (byte[]) myReader["ImageData"];
}
else
myContentType = "";

myReader.Close();
myCommand.Dispose();
return Img;
}
finally
{
myCon.Close();
}
}
 
Back
Top