473,386 Members | 1,598 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,386 software developers and data experts.

How can i render the multiple images from database?

Hi i am newbie in asp.net i am having problem regarding rendering multiple images from database.
Here is my code for single image retreivation.
''''
Expand|Select|Wrap|Line Numbers
  1. Partial Public Class dspimg
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         Response.ContentType = "image/jpg"
  6.         Using dbcon As New SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=Employee;Integrated Security=True;")
  7.             Dim id As Integer
  8.             Dim sqlCom As New SqlCommand
  9.             Integer.TryParse(Request.QueryString("ID"), id)
  10.             sqlCom.Connection = dbcon
  11.             sqlCom.CommandType = CommandType.Text
  12.             sqlCom.CommandText = "SELECT img FROM PIC" + "WHERE id=@ImageID"
  13.             sqlCom.Parameters.Add("@ImageID", SqlDbType.Int).Value = id
  14.  
  15.  
  16.             ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  17.  
  18.             Try
  19.  
  20.                 dbcon.Open()
  21.                 Dim imgContents As Byte() = DirectCast(sqlCom.ExecuteScalar(), Byte())
  22.  
  23.                 'Now that we have the image, writing it to the Response's Output Stream 
  24.                 Response.BinaryWrite(imgContents)
  25.  
  26.             Catch ex As Exception
  27.                 MsgBox(ex.Message)
  28.                 'Something went wrong connecting to the Database 
  29.             Finally
  30.                 'Ensuring that the database connection is closed 
  31.                 'Even if something went wrong 
  32.                 dbcon.Close()
  33.             End Try
  34.         End Using
  35.     End Sub
*Below is my database Script.
Expand|Select|Wrap|Line Numbers
  1. USE [Employee]
  2. GO
  3. /****** Object:  Table [dbo].[PIC]    Script Date: 12/05/2011 03:29:09 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. CREATE TABLE [dbo].[PIC](
  9.     [id] [numeric](18, 0) NULL,
  10.     [img] [image] NULL
  11. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Dec 4 '11 #1
2 3726
Its my humble request to experts that kindly reply me.
Dec 4 '11 #2
Frinavale
9,735 Expert Mod 8TB
You can't return multiple images to the browser because it won't know how to display them. You have to return one image at a time.

I would use a Repeater or a GridView for this.

For example, I would have this simple repeater in my page:
Expand|Select|Wrap|Line Numbers
  1. <asp:Repeater ID="imageDisplayer" runat="server">
  2.     <ItemTemplate>
  3.         <asp:Image ImageUrl='<%#Eval("ImageURL") %>' runat="server"/>
  4.     </ItemTemplate>
  5. </asp:Repeater>
This Repeater will use an Image Control for each row of data that it is bound to. That Image Control is bound to a column called "ImageURL" which contains the URL to the Image that the Image Control is going to display.


In my C# code for the page I would bind this repeater to the list of images.

Like this:
Expand|Select|Wrap|Line Numbers
  1. private System.Data.DataTable dataSource;
  2.  
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5.     if (Session["dataSource"] == null)
  6.     {
  7.         dataSource = GenerateDataSource();
  8.         Session["dataSource"] = dataSource;
  9.     }
  10.     else
  11.     {
  12.         dataSource = (System.Data.DataTable)Session["dataSource"];
  13.     }
  14.     this.Page.PreRender += new EventHandler(Page_PreRender);
  15. }
  16.  
  17. void Page_PreRender(object sender, EventArgs e)
  18. {
  19.     imageDisplayer.DataSource = dataSource;
  20.     imageDisplayer.DataBind();
  21. }
  22.  
  23. private System.Data.DataTable GenerateDataSource()
  24. {
  25. //In here you want to retrieve all of the IDs of the
  26. //images in your database instead of using file names
  27. //you want to pass this ID to the aspx page that retrieves the
  28. //individual image to display in the image control.
  29.     System.Data.DataTable dt = new System.Data.DataTable();
  30.     dt.Columns.Add(new System.Data.DataColumn("ImageURL"));
  31.     string[] filenames = { "1.png", "2.png", "3.png", "4.png", "5.png", "6.png" };
  32.     foreach (string fileName in filenames)
  33.     {
  34.         System.Data.DataRow dr = dt.NewRow();
  35.         dr[0] = "Images/" + fileName;
  36.         dt.Rows.Add(dr);
  37.     }
  38.  
  39.     return dt;
  40. }

In your case you want the Image URL to point to the ASP.NET page that retrieves the image from the database. You want to add the ID of the image you want to display to the QueryString so that you can retrieve the image from your database with this ID in the ASPX page that retrieves the image for you.

-Frinny
Dec 5 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Patrick Blackman | last post by:
How do you create a TIFF to store multiple images?
2
by: Netian | last post by:
Hello , Please teach me how to create an Icon object with multiple images by GDI++ . I can create an Icon with a image as following , but can't do an Icon with multiple images. private Icon...
2
by: Jon | last post by:
Hello all, Can anyone offer any help with our problem. We have a database that returns multiple (100's) of images as memorystreams. We need to output these all at once. The option of having a...
0
by: Just Jeff | last post by:
Hi, I am a newbie to CSS. My problem at this point has to do with layout. I've inherited a website that uses nested tables for the layout of the page (www.gccnh.com), which I would like to...
11
by: Jankie | last post by:
I need to dispaly a user's multiple images in one entry.Right now,say if a user uploads 3 images,three entries for the same id display to match 3 images. I only want 1 entry to display all of a...
5
by: swethak | last post by:
Hi,. when i display the image by using below code i got the error as image displayed in the form of garbage value format.plz tell that what's the mistake in my code. <?php...
1
by: =?Utf-8?B?UmFqYWdvcGFs?= | last post by:
Question How to Upload multiple images in asp.net? (ex.Like a gmail file field)
2
by: sarayu | last post by:
Hi All, How can i select multiple images by using shift or control key from my hard disk and upload it by using a submit button.For this what we use for selecting different images and...
0
selvasoft
by: selvasoft | last post by:
Hi Please help me any one. I want solution for display multiple images from oracle database.Using JSP. here is my code for display one image from database. Please Any one give me some ideas. ...
3
KeredDrahcir
by: KeredDrahcir | last post by:
I want to be able the upload multiple images at once. I can do a normal upload where the user browses for each image individually and I can also do where you have several browse images on the screen....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
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...
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...

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.