468,133 Members | 1,433 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

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 2343
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

Post your reply

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

Similar topics

2 posts views Thread by Tjerk | last post: by
2 posts views Thread by Al Reid | last post: by
reply views Thread by csgraham74 | last post: by
reply views Thread by csgraham74 | last post: by
reply views Thread by csgraham74 | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.