Display Image From Database row

Posted on: Thu Oct 30 14:24:40 -0700 2008. Updated on: Thu Oct 30 14:33:34 -0700 2008.
Category: DotNet

Assume we have a database table with an ID and a Image Datatype column, plus maybe some other info. We will add a WebForm that will be used to display the image. We can call it something like DisplayImage.aspx and it will be passed a Querystring "id" to know which record from the database table to get. Then, wherever we need to display an image, we just set the ImageUrl property of an asp Image control to "~/DisplayImage.aspx?id=1234"

First let's build the DisplayImage.aspx file. Go into the codebehind and do something like this:

protected void Page_Load(object sender, EventArgs e)
{
   if (!String.IsNullOrEmpty(Request.QueryString["id"]))
   {
      int logoId = Convert.ToInt32(Request.QueryString["id"]);
      project.LogosSvc webservice = new project.subSvc.LogosSvc();

      sbSvc.DSTemplates.logosDataTable table = webservice.GetLogoById(logoId);

      if (table.Count > 0)
      {
         Response.ContentType = table[0].mime_type;
	 Response.BinaryWrite(table[0].logo);
      }
   }
}

Now in any other file where you want to use the image with that record from DB, then simply do something like this in the cs. (First add asp Image control in .aspx webform and give it ID ImageLogo)

int logoId = 123;
ImageLogo.ImageUrl = String.Format("~/DisplayImage.aspx?id={0}",logoId);

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.