473,748 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="multip art/form-data"
runat="server">
<INPUT id="UploadedIma geFile" runat=server type="file" size="86">
</form>
Codebehind
----------
Private Sub InsertIntoDB()

'then get the stream
Dim MyStream As System.IO.Strea m

MyStream = UploadedImageFi le.PostedFile.I nputStream
'MyStream = upImage.InputSt ream
'Dim MyData(MyStream .Length) As Byte
Dim MyData(Uploaded ImageFile.Poste dFile.InputStre am.Length) As
Byte

Dim n = MyStream.Read(M yData, 0,
UploadedImageFi le.PostedFile.I nputStream.Leng th)

Dim CultivarImagesR ow As dsCultivarVisio n.CultivarImage sRow =
ds.CultivarImag es.NewCultivarI magesRow
With CultivarImagesR ow
.CultivarId = lbCultivarName. SelectedValue
.ImageClassific ationId =
ddImageClassifi cation.Selected Value
.ImageUseId = ddImageUse.Sele ctedValue
.ImageFormatId = ddImageFormat.S electedValue
.ImageDescripti on = txtImageDescrip tion.Text
.Credit = txtImageCredit. Text
.ImageBinary = MyData
Dim conn As New
SqlConnection(A pplication("Con nectionString") )
conn.Open()

Dim connInsert As New SqlCommand _
("Insert INTO XCultivarImages (.CultivarID,
..ImageClassifi cationID, .ImageUseId, .ImageFormatID,

..ImageDescript ion, .Credit, .ImageBinary) values(@Cultiva rID,
@ImageClassific ationID, @ImageUseId,

@ImageFormatID, @ImageDescripti on, @Credit, @ImageBinary)", conn)

connInsert.Para meters.Add(New SqlParameter("@ CultivarID",
SqlDbType.Int, 4, "CultivarID "))
connInsert.Para meters.Add(New
SqlParameter("@ ImageClassifica tionID", SqlDbType.Int, 4,

"ImageClassific ationID"))
connInsert.Para meters.Add(New SqlParameter("@ ImageUseID",
SqlDbType.Int, 4, "ImageUseID "))
connInsert.Para meters.Add(New
SqlParameter("@ ImageFormatID", SqlDbType.Int, 4, "ImageFormatID" ))
connInsert.Para meters.Add(New
SqlParameter("@ ImageDescriptio n", SqlDbType.Text, 16,

"ImageDescripti on"))
connInsert.Para meters.Add(New SqlParameter("@ Credit",
SqlDbType.VarCh ar, 65, "Credit"))
connInsert.Para meters.Add(New SqlParameter("@ ImageBinary",
SqlDbType.Image , 16, "ImageBinar y"))

connInsert.Para meters("@Cultiv arId").Value = .CultivarId
connInsert.Para meters("@ImageC lassificationID ").Value =
..ImageClassifi cationId
connInsert.Para meters("@ImageU seID").Value = .ImageUseId
connInsert.Para meters("@ImageF ormatID").Value =
..ImageFormatId
connInsert.Para meters("@ImageD escription").Va lue =
..ImageDescript ion
connInsert.Para meters("@Credit ").Value = .Credit
connInsert.Para meters("@ImageB inary").Value = .ImageBinary
connInsert.Exec uteNonQuery()

conn.Close()
End With



End Sub
On my aspx that should display the image I have the following tag:
<asp:Image ID="imgCultivar "
ImageUrl="displ ayimage.aspx?cu ltivarid=16775" width=200 height=200

Runat="server"> </asp:Image>

On displayimage.as px.vb I have the following code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim intcultivarid = Request.QuerySt ring("cultivari d")

Dim sqltext = "SELECT imagebinary from xcultivarimages where
cultivarid = " & intcultivarid
Dim conn As New SqlConnection(A pplication("Con nectionString") )
Dim command As New SqlCommand(sqlt ext, conn)

conn.Open()
Dim dr As SqlDataReader = command.Execute Reader

Do While (dr.Read())
If Not dr(0) Is System.DBNull.V alue Then

Response.Conten tType = "Image/jpeg"
Response.Binary Write(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 2105
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.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
if(Request["ImageID"] != null)
{
int ImgID = int.Parse(Reque st["ImageID"]);
CodePlayDB myCode = new CodePlayDB();
string ContentType = "";
byte[] Image = myCode.GetImage FromDB(ImgID, ref ContentType);
Response.Conten tType = ContentType;
Response.Binary Write(Image);
}
}
}

Code to retrieve it from db
public byte[] GetImageFromDB( int myImageID, ref string myContentType)
{
SqlConnection myCon = new
SqlConnection(C onfigurationSet tings.AppSettin gs["ConnectionStri ng"]);
try
{
SqlCommand myCommand = new SqlCommand("sp_ Images_Select", myCon);
myCommand.Comma ndType = CommandType.Sto redProcedure;

SqlParameter parameterImageI D = new SqlParameter("@ ImageID",
SqlDbType.Int, 4);
parameterImageI D.Value = myImageID;
myCommand.Param eters.Add(param eterImageID);

myCon.Open();
SqlDataReader myReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection);
byte[] Img = null;
if(myReader.Rea d())
{
myContentType = myReader["ImageContentTy pe"].ToString();
Img = (byte[]) myReader["ImageData"];
}
else
myContentType = "";

myReader.Close( );
myCommand.Dispo se();
return Img;
}
finally
{
myCon.Close();
}
}

"Adam" <ak************ **@yahoo.com> wrote in message
news:23******** *************** ***@posting.goo gle.com...
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="multip art/form-data"
runat="server">
<INPUT id="UploadedIma geFile" runat=server type="file" size="86">
</form>
Codebehind
----------
Private Sub InsertIntoDB()

'then get the stream
Dim MyStream As System.IO.Strea m

MyStream = UploadedImageFi le.PostedFile.I nputStream
'MyStream = upImage.InputSt ream
'Dim MyData(MyStream .Length) As Byte
Dim MyData(Uploaded ImageFile.Poste dFile.InputStre am.Length) As
Byte

Dim n = MyStream.Read(M yData, 0,
UploadedImageFi le.PostedFile.I nputStream.Leng th)

Dim CultivarImagesR ow As dsCultivarVisio n.CultivarImage sRow =
ds.CultivarImag es.NewCultivarI magesRow
With CultivarImagesR ow
.CultivarId = lbCultivarName. SelectedValue
.ImageClassific ationId =
ddImageClassifi cation.Selected Value
.ImageUseId = ddImageUse.Sele ctedValue
.ImageFormatId = ddImageFormat.S electedValue
.ImageDescripti on = txtImageDescrip tion.Text
.Credit = txtImageCredit. Text
.ImageBinary = MyData
Dim conn As New
SqlConnection(A pplication("Con nectionString") )
conn.Open()

Dim connInsert As New SqlCommand _
("Insert INTO XCultivarImages (.CultivarID,
.ImageClassific ationID, .ImageUseId, .ImageFormatID,

.ImageDescripti on, .Credit, .ImageBinary) values(@Cultiva rID,
@ImageClassific ationID, @ImageUseId,

@ImageFormatID, @ImageDescripti on, @Credit, @ImageBinary)", conn)

connInsert.Para meters.Add(New SqlParameter("@ CultivarID",
SqlDbType.Int, 4, "CultivarID "))
connInsert.Para meters.Add(New
SqlParameter("@ ImageClassifica tionID", SqlDbType.Int, 4,

"ImageClassific ationID"))
connInsert.Para meters.Add(New SqlParameter("@ ImageUseID",
SqlDbType.Int, 4, "ImageUseID "))
connInsert.Para meters.Add(New
SqlParameter("@ ImageFormatID", SqlDbType.Int, 4, "ImageFormatID" ))
connInsert.Para meters.Add(New
SqlParameter("@ ImageDescriptio n", SqlDbType.Text, 16,

"ImageDescripti on"))
connInsert.Para meters.Add(New SqlParameter("@ Credit",
SqlDbType.VarCh ar, 65, "Credit"))
connInsert.Para meters.Add(New SqlParameter("@ ImageBinary",
SqlDbType.Image , 16, "ImageBinar y"))

connInsert.Para meters("@Cultiv arId").Value = .CultivarId
connInsert.Para meters("@ImageC lassificationID ").Value =
.ImageClassific ationId
connInsert.Para meters("@ImageU seID").Value = .ImageUseId
connInsert.Para meters("@ImageF ormatID").Value =
.ImageFormatId
connInsert.Para meters("@ImageD escription").Va lue =
.ImageDescripti on
connInsert.Para meters("@Credit ").Value = .Credit
connInsert.Para meters("@ImageB inary").Value = .ImageBinary
connInsert.Exec uteNonQuery()

conn.Close()
End With



End Sub
On my aspx that should display the image I have the following tag:
<asp:Image ID="imgCultivar "
ImageUrl="displ ayimage.aspx?cu ltivarid=16775" width=200 height=200

Runat="server"> </asp:Image>

On displayimage.as px.vb I have the following code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim intcultivarid = Request.QuerySt ring("cultivari d")

Dim sqltext = "SELECT imagebinary from xcultivarimages where
cultivarid = " & intcultivarid
Dim conn As New SqlConnection(A pplication("Con nectionString") )
Dim command As New SqlCommand(sqlt ext, conn)

conn.Open()
Dim dr As SqlDataReader = command.Execute Reader

Do While (dr.Read())
If Not dr(0) Is System.DBNull.V alue Then

Response.Conten tType = "Image/jpeg"
Response.Binary Write(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
1821
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 moved to the user's area. The user's area for images is at users/the_username/photo/ where users is a directory off the root. The staging area is tmp_photo/, also off the root. When it is in the staging area, I see the image. Using identical...
0
1803
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 getting the picture resized and thumbnailed... I'm on a apache server running php 5 with 8MB php_memory. When uploading, the script works fine most times when uploading small files (below 100kb and sometimes up close to 300kb too), but when...
0
2296
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 tab, I am loosing the file in the file control. So, I am putting the file in a Session: Session("PhotoFile") = filePhoto.PostedFile When the user comes back to the tab with the File upload, an Image control
7
11638
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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
15
5362
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
2
6964
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
2
2365
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 when you upload image it will go to another page and says ]((unable to create emp directory)) Here is a site to be able to see script actually work http://tech.tailoredweb.com/image-editor-52/ and can be DL from there also. I am using FP 2003 and...
9
2593
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" runat="server"/> <asp:Label ID="CompanyPicture" runat="server"/> I have 2 links that open the windows to preview these images. The previewed images are done on separate html pages that do nothing but display the
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
6799
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.