Retrieving Images stored in SQL Server 2005

G

Guest

I am retrieving images that I stored in SQL Server on my web pages in C#. I
have no problem with the images displaying, however, I am trying to wrap the
image with an <A HREF ..." and each time I try, it acts like the link is not
even on the image.

What is the proper way to do this?

Thanks.
 
S

S. Justin Gengo

Sirplaya,

Would you be able to show us both the portion of code that is to produce the
<a href, and the viewstate of the page so we may see what that code
produces?

Thanks,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
C

Cowboy \(Gregory A. Beamer\)

Try an ImageButrton. Much easier than wrapping controls in HTML.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
G

Guest

Sure,

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Hello there</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data"
method="post">
<a href='#'><asp:Image ID="imgProdItem" runat="server" /></a>
</form>
</body>
</html>

code-behind:

protected void ShowCurrentPic()
{
SqlConnection oConn = new SqlConnection(CONN_STRING);
SqlCommand oCmd = new SqlCommand();
SqlDataReader oDr;
string strCatID = Request.QueryString["cat.id"].ToString();
string strSubID = "";
if(Request.QueryString["sub.id"] == null)
{
strSubID = "0";
}
else
{
strSubID = Request.QueryString["sub.id"].ToString();
}

string sImgQuery = "SELECT img_data, img_name, img_contenttype " +
"FROM tbl_FeatureBoxes " +
"WHERE CategoryID = " + strCatID + " AND " +
"SubCategoryID = " + strSubID + " AND " +
"BoxName = 'Online'";

oCmd.CommandText = sImgQuery;
oCmd.CommandType = CommandType.Text;
oCmd.Connection = oConn;

oConn.Open();
oDr = oCmd.ExecuteReader();
if (oDr.Read())
{
string img_data = oDr["img_data"].ToString();
if (img_data == "")
{
this.imgProdItem.ImageUrl = "~/images/img_niu.gif";
}
else
{
byte[] byteArray = (byte[])oDr["img_data"];
Response.ContentType = oDr["img_contenttype"].ToString();
Response.BinaryWrite(byteArray);
}
}

oDr.Dispose();
oCmd.Dispose();
oConn.Close();
}

-------

I want to also note that the page that holds my .net image control is the
only thing on the page. When I try to add anything else on the page, it is
never displayed/showed. No matter what I put on that page, the only thing
visible is the image from the SQL server.
 
G

Guest

I forgot to mention that when I try to view the source of the page, I cannot
because it is not available. it almost appears as though I am just viewing
the image and not the page itself.
 
C

Cowboy \(Gregory A. Beamer\)

Overall, I am not fond of storing images in the database, as it is easier to
store in the file system. But, there are times you have to do it. Look at
the AdventureWorks and Northwind samples to see how to stream images out. If
I remember correctly, one of the .NET examples displays employee images and
allows you to click to an information page on the employee. Thta is what I
would look for if you are having difficulties.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
S

S. Justin Gengo

Sirplaya,

You're giving your image a file url but then streaming the image in a binary
stream directly to the page requested. Your writing your image out in a
binary stream to the page is replacing the page's normal content with the
stream. Here's what you need to do:

1) Have your original page make a request to another .aspx page for the
image data. That request should include which image to get in the
querystring.

2) When you get the image out of SQL Server you need to do so in a separate
..aspx page and then use that page's response to stream the binary data.

I have a sample of how to do this in an image manipulation sample on my
website:

http://www.aboutfortunate.com/default.aspx?page=imagemanipulator

This sample program stores the image in memory on the server in order to
manipulate it (change to .jpg, etc.)

While it is not storing the image in sql server it will show you what you
need to know. Pay special attention to how the ImageDelivery.aspx page
streams the image out. This is where you'll want to take the code and modify
it to use your sql statement and binary write.

Then also look at the ImageUpload.aspx page and pay special attention to how
the ImagePreview image control's ImageUrl property is set. You'll find this
at the very end of the "DisplayPreview" subroutine.

Regards,



--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche


Sirplaya said:
Sure,

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Hello there</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data"
method="post">
<a href='#'><asp:Image ID="imgProdItem" runat="server" /></a>
</form>
</body>
</html>

code-behind:

protected void ShowCurrentPic()
{
SqlConnection oConn = new SqlConnection(CONN_STRING);
SqlCommand oCmd = new SqlCommand();
SqlDataReader oDr;
string strCatID = Request.QueryString["cat.id"].ToString();
string strSubID = "";
if(Request.QueryString["sub.id"] == null)
{
strSubID = "0";
}
else
{
strSubID = Request.QueryString["sub.id"].ToString();
}

string sImgQuery = "SELECT img_data, img_name, img_contenttype " +
"FROM tbl_FeatureBoxes " +
"WHERE CategoryID = " + strCatID + " AND " +
"SubCategoryID = " + strSubID + " AND " +
"BoxName = 'Online'";

oCmd.CommandText = sImgQuery;
oCmd.CommandType = CommandType.Text;
oCmd.Connection = oConn;

oConn.Open();
oDr = oCmd.ExecuteReader();
if (oDr.Read())
{
string img_data = oDr["img_data"].ToString();
if (img_data == "")
{
this.imgProdItem.ImageUrl = "~/images/img_niu.gif";
}
else
{
byte[] byteArray = (byte[])oDr["img_data"];
Response.ContentType = oDr["img_contenttype"].ToString();
Response.BinaryWrite(byteArray);
}
}

oDr.Dispose();
oCmd.Dispose();
oConn.Close();
}

-------

I want to also note that the page that holds my .net image control is the
only thing on the page. When I try to add anything else on the page, it is
never displayed/showed. No matter what I put on that page, the only thing
visible is the image from the SQL server.

S. Justin Gengo said:
Sirplaya,

Would you be able to show us both the portion of code that is to produce
the
<a href, and the viewstate of the page so we may see what that code
produces?

Thanks,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top