473,385 Members | 1,320 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,385 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 2667
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 anybody have an idea how I can make it work again or...
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 SQL Server database, in a table called...
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, could someone tell me how to do it? TIA -- Al...
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 large image with the small image when i...
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 replace the large image with the small image when i...
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 large image with the small image when i...
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 to a column in the datagrid what i'd like to do...
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. Access2000. I have one table and a bound single...
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 the local system.But when hosted on server,both the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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...

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.