Display Image From Database row
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);