Dynamic Image from Database (Post 2)

J

Joe Reiss

Hi All,

Using vs2005, C#, SQL Server 2005/Shared Environment.

This is my second Post since I got absolutely no responses the first time.
I really need to solve this one.

I'm attempting to populate an image control from a jpeg stored in a image
field in sql server. Tried using both the HTML image (runat = "server") and
ASP image controls - can't get
either to work.

I'm basically using the technique outlined in the ASP.NET 2.0 Cookbook
(OReilly) and many other places and articles.

So, in my page that has the image controls the code in the view photo button
event is:

img.Src = "LoadPhoto.aspx?ID=7";
img.visible = true;

The LoadPhoto.aspx that gets the jpeg data to render has no HTML just the
'Page, AutoEventWireup,
CodeFile,Inherits' as specified for this technique. The codebehind for
LoadPhoto.aspx.cs has the Page_Load event where it gets the jpeg data and
calls Response.ContentType = "image/jpeg" then Response.BinaryWrite( calls
function that returns byte[] array from database).

I cannot get this to work. Also, I put in some code into LoadPhoto.aspx.cs
to populate a session varible just to see if the Page_Load event is firing.
The
session variable does not get populated, so, it seems that Page_Load event
doesn't event fire.

Can someone help me out?

Thanks,
Joe
 
D

David Dixon

Joe,

You are obviously reading the wrong books. This sample code is taken from
SAMS website :
http://www.samspublishing.com/articles/article.asp?p=377078&rl=1

David
www.dxnuk.com

public void Page_Load(System.Object sender, System.EventArgs e) {
try
{
System.Int32 _ImgID = System.Convert.ToInt32(Request.QueryString["ID"]);
System.Data.SqlClient.SqlConnection Con = new
System.Data.SqlClient.SqlConnection( "Data
Source=216.221.103.167;database=e-commerce;uid=webappsx;pwd=tyqldf" );
System.String SqlCmd = "SELECT * FROM Image WHERE img_pk = @ImageID";
System.Data.SqlClient.SqlCommand SqlCmdObj = new
System.Data.SqlClient.SqlCommand( SqlCmd, Con );
SqlCmdObj.Parameters.Add("@ImageID", System.Data.SqlDbType.Int).Value =
_ImgID;
Con.Open();
System.Data.SqlClient.SqlDataReader SqlReader = SqlCmdObj.ExecuteReader();
SqlReader.Read();
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream( new
System.IO.MemoryStream( (byte[])SqlReader["img_data"] ) );
_image.Save( System.Web.HttpContext.Current.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg );
}
catch (System.Exception Ex)
{
System.Web.HttpContext.Current.Trace.Write(Ex.Message.ToString());
}
 
J

Joe Reiss

No matter the code my Page_Load event is not firing - any other ideas?

thnx,
Joe

David Dixon said:
Joe,

You are obviously reading the wrong books. This sample code is taken from
SAMS website :
http://www.samspublishing.com/articles/article.asp?p=377078&rl=1

David
www.dxnuk.com

public void Page_Load(System.Object sender, System.EventArgs e) {
try
{
System.Int32 _ImgID = System.Convert.ToInt32(Request.QueryString["ID"]);
System.Data.SqlClient.SqlConnection Con = new
System.Data.SqlClient.SqlConnection( "Data
Source=216.221.103.167;database=e-commerce;uid=webappsx;pwd=tyqldf" );
System.String SqlCmd = "SELECT * FROM Image WHERE img_pk = @ImageID";
System.Data.SqlClient.SqlCommand SqlCmdObj = new
System.Data.SqlClient.SqlCommand( SqlCmd, Con );
SqlCmdObj.Parameters.Add("@ImageID", System.Data.SqlDbType.Int).Value =
_ImgID;
Con.Open();
System.Data.SqlClient.SqlDataReader SqlReader = SqlCmdObj.ExecuteReader();
SqlReader.Read();
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream( new
System.IO.MemoryStream( (byte[])SqlReader["img_data"] ) );
_image.Save( System.Web.HttpContext.Current.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg );
}
catch (System.Exception Ex)
{
System.Web.HttpContext.Current.Trace.Write(Ex.Message.ToString());
}



Joe Reiss said:
Hi All,

Using vs2005, C#, SQL Server 2005/Shared Environment.

This is my second Post since I got absolutely no responses the first
time.
I really need to solve this one.

I'm attempting to populate an image control from a jpeg stored in a image
field in sql server. Tried using both the HTML image (runat = "server") and
ASP image controls - can't get
either to work.

I'm basically using the technique outlined in the ASP.NET 2.0 Cookbook
(OReilly) and many other places and articles.

So, in my page that has the image controls the code in the view photo button
event is:

img.Src = "LoadPhoto.aspx?ID=7";
img.visible = true;

The LoadPhoto.aspx that gets the jpeg data to render has no HTML just the
'Page, AutoEventWireup,
CodeFile,Inherits' as specified for this technique. The codebehind for
LoadPhoto.aspx.cs has the Page_Load event where it gets the jpeg data and
calls Response.ContentType = "image/jpeg" then Response.BinaryWrite(
calls
function that returns byte[] array from database).

I cannot get this to work. Also, I put in some code into LoadPhoto.aspx.cs
to populate a session varible just to see if the Page_Load event is firing.
The
session variable does not get populated, so, it seems that Page_Load
event
doesn't event fire.

Can someone help me out?

Thanks,
Joe
 
J

Joe Reiss

Got it, the AutoEventWireup needs to be 'true' otherwise the Page_load
doesn't fire in a 'htm' - less page whose only purpose is to stream back an
image.

Joe


Joe Reiss said:
No matter the code my Page_Load event is not firing - any other ideas?

thnx,
Joe

David Dixon said:
Joe,

You are obviously reading the wrong books. This sample code is taken
from
SAMS website :
http://www.samspublishing.com/articles/article.asp?p=377078&rl=1

David
www.dxnuk.com

public void Page_Load(System.Object sender, System.EventArgs e) {
try
{
System.Int32 _ImgID = System.Convert.ToInt32(Request.QueryString["ID"]);
System.Data.SqlClient.SqlConnection Con = new
System.Data.SqlClient.SqlConnection( "Data
Source=216.221.103.167;database=e-commerce;uid=webappsx;pwd=tyqldf" );
System.String SqlCmd = "SELECT * FROM Image WHERE img_pk = @ImageID";
System.Data.SqlClient.SqlCommand SqlCmdObj = new
System.Data.SqlClient.SqlCommand( SqlCmd, Con );
SqlCmdObj.Parameters.Add("@ImageID", System.Data.SqlDbType.Int).Value =
_ImgID;
Con.Open();
System.Data.SqlClient.SqlDataReader SqlReader =
SqlCmdObj.ExecuteReader();
SqlReader.Read();
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream( new
System.IO.MemoryStream( (byte[])SqlReader["img_data"] ) );
_image.Save( System.Web.HttpContext.Current.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg );
}
catch (System.Exception Ex)
{
System.Web.HttpContext.Current.Trace.Write(Ex.Message.ToString());
}



Joe Reiss said:
Hi All,

Using vs2005, C#, SQL Server 2005/Shared Environment.

This is my second Post since I got absolutely no responses the first
time.
I really need to solve this one.

I'm attempting to populate an image control from a jpeg stored in a
image
field in sql server. Tried using both the HTML image (runat = "server") and
ASP image controls - can't get
either to work.

I'm basically using the technique outlined in the ASP.NET 2.0 Cookbook
(OReilly) and many other places and articles.

So, in my page that has the image controls the code in the view photo button
event is:

img.Src = "LoadPhoto.aspx?ID=7";
img.visible = true;

The LoadPhoto.aspx that gets the jpeg data to render has no HTML just
the
'Page, AutoEventWireup,
CodeFile,Inherits' as specified for this technique. The codebehind for
LoadPhoto.aspx.cs has the Page_Load event where it gets the jpeg data
and
calls Response.ContentType = "image/jpeg" then Response.BinaryWrite(
calls
function that returns byte[] array from database).

I cannot get this to work. Also, I put in some code into LoadPhoto.aspx.cs
to populate a session varible just to see if the Page_Load event is firing.
The
session variable does not get populated, so, it seems that Page_Load
event
doesn't event fire.

Can someone help me out?

Thanks,
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top