473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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.Le ngth) 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").Va lue =
..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
Nov 18 '05 #1
1 2078
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.CloseConne ction);
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();
}
}

"Adam" <ak**************@yahoo.com> wrote in message
news:23**************************@posting.google.c om...
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.Le ngth) 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").Va lue =
.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

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shelly | last post by:
This is driving me crazy and makes no sense. Any suggestions will be appreciated. I upload an image to a staging area. I have written software to look at the images, and if accepted it is...
0
by: doffer | last post by:
I want to make a portfoliosystem where user can register and get their own portfolio... I've started the developer work, but I'm stuck on the image upload part... I'm experiencing some problems...
0
by: Satish Appasani | last post by:
Hi: I have a ASP.NET form with Web layout which I've achieved using panels. In one of the tab I have a File control to upload Images. When I put a file in the file control and move to another...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
2
by: Poppa Pimp | last post by:
ImageResizer.php Image Resizer PLEASE HELP The URL of the page this is on in my site is http://poppa-pimps-wallpapers.com//ImageResizer.php You can click on browse and get image,but...
9
by: tshad | last post by:
This was posted before but the message got messed up (all NLs were stripped out for some reason). I have 2 labels that hold the name of different images on my .aspx page. <asp:Label ID="Logo"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.