472,326 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Datagrid Image Displaying issue

Hello,

So I've read many forums that explained how to get an image out of SQL Server database and display it. I used an alternate .aspx page to retrieve the image and write it back as binary which everyone on many sites said to do. The problem is its not working.

My exact situation is that I am creating News Articles based on news entries which are either text or pictures. I want to display a picture in the datagrid if there is one. Currently I get the red box with the x for everything (even when there is supposed to be a picture!!!)

I'm Using C# and ASP.NET 1.1

my datagrid has this kind of column for it:

Expand|Select|Wrap|Line Numbers
  1. <asp:TemplateColumn>
  2.                                 <ItemTemplate>
  3.                                     <table align="center" width="100%">
  4.  
  5.                                         <tr>
  6.                                             <td class="newstext"><%# DataBinder.Eval(Container.DataItem, "theText")%></td>
  7.                                         </tr>
  8.  
  9.                                         <!-- <tr>
  10.                                             <td><img src="getImage.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "imageID") %>&type=<%# DataBinder.Eval(Container.DataItem, "contentType") %>" id="Img1" /></td>
  11.                                         </tr> -->
  12.  
  13.                                     </table>
  14.                                 </ItemTemplate>
  15.  
  16.                             </asp:TemplateColumn>
  17.  
My getImage.aspx file has this code in it:

Expand|Select|Wrap|Line Numbers
  1. if(Request.QueryString["type"].ToString().Equals("picture"))
  2.             {
  3.                 System.Data.SqlClient.SqlDataReader Reader=null;
  4.                 string ImageIdentity = Request.QueryString["ID"].ToString();
  5.                 SqlConnection sqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["Connection"]);
  6.  
  7.  
  8.                 sqlConnection.Open();    
  9.                 SqlCommand sqlCommand = new SqlCommand("Select theImage from TAPSite.dbo.NewsImages where imageID="+ImageIdentity  ,sqlConnection);
  10.                 Reader= sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
  11.  
  12.  
  13.                 Reader.Read();
  14.                 byte [] byteArray = (byte[]) Reader["theImage"];
  15.  
  16.                 Response.ContentType=Reader["imageType"].ToString();
  17.                 Response.BinaryWrite(byteArray);
  18.                 sqlConnection.Close();
  19.             }
  20.             else
  21.             {
  22.                 System.Web.UI.WebControls.Image spacer = new System.Web.UI.WebControls.Image();
  23.                 spacer.ImageUrl = "spacer.gif";
  24.                 ImageConverter ic = new ImageConverter();
  25.  
  26.                 Response.ContentType= "image/gif";
  27.                 // Byte[] imageByteArray = (byte[]) converter.ConvertTo(im, typeof(byte[]));
  28.                 byte[] spaceArray = (byte[])ic.ConvertTo(spacer, typeof(byte[]));
  29.                 Response.BinaryWrite(spaceArray);
  30.             }
  31.  
it is meant either send back the image from the database or a spacer.

and finally my ItemDataBound function for my datagrid has this code in it :

Expand|Select|Wrap|Line Numbers
  1. if((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
  2.             {
  3.                 if(e.Item.Cells[1].Equals("picture"))
  4.                 {
  5.                     System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
  6.                     img.ImageUrl = "getImage.aspx?ID=" + e.Item.Cells[5] + "&type=picture";
  7.                     e.Item.Controls.Add(img);
  8.  
  9.                 }
  10.                 else if(e.Item.Cells[1].Equals("text"))
  11.                 {
  12.  
  13.                 }
  14.             }
  15.  
Sorry for the long post, but I really really need help with this.

Thanks,

-Mike
Jul 10 '07 #1
1 2513
I fixed it. I put a conditional operator inline the img src= line that chose between linking to getImage.aspx and a spacer image i made.

Its really more of a workaround but whatever.
Jul 10 '07 #2

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

Similar topics

2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a...
2
by: Al Reid | last post by:
Is it possible to display an image that is stored on the server as a TIFF image, on an ASP.Net page without the use of an add-in viewer? If so,...
0
by: csgraham74 | last post by:
Hello, Ive created a datagrid and inserted a table for displaying images. i have four small images and one large image. I want to replace the...
0
by: csgraham74 | last post by:
Hello, Ive created a datagrid and inserted a html table for displaying images. i have four small images and one large image. I want to...
0
by: csgraham74 | last post by:
Hello, Ive created a datagrid and inserted a table for displaying images. i have four small images and one large image. I want to replace...
4
by: simon | last post by:
hello, i am displaying a dataset in a datagrid, for one of the values being displayed it either comes back as a 1 or a 0, which is currently bound...
1
by: ApexData | last post by:
Hello I am in the early stages of my project development, and want to make sure that I am approaching the issue of image display correctly. ...
3
by: mvijayrkumar | last post by:
Hi to all.... Guys pls help me..... I have an image issue in RLDC while hosting my project on server.The image displays or works fine with...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.