By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,909 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,909 IT Pros & Developers. It's quick & easy.

Retrieving Images stored in SQL Server 2005

P: n/a
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.
Jul 8 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
>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.

Jul 8 '06 #2

P: n/a
Try an ImageButrton. Much easier than wrapping controls in HTML.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
>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.

Jul 8 '06 #3

P: n/a
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" wrote:
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
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
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.


Jul 8 '06 #4

P: n/a
Hi Greg,

I tried that method too a bit earlier to no avail.

"Cowboy (Gregory A. Beamer)" wrote:
Try an ImageButrton. Much easier than wrapping controls in HTML.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
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.


Jul 8 '06 #5

P: n/a
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.

"S. Justin Gengo" wrote:
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
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
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.


Jul 8 '06 #6

P: n/a
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!
*************************************************
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
Hi Greg,

I tried that method too a bit earlier to no avail.

"Cowboy (Gregory A. Beamer)" wrote:
>Try an ImageButrton. Much easier than wrapping controls in HTML.

--
Gregory A. Beamer

*********************************************** **
Think Outside the Box!
*********************************************** **
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microso ft.com...
>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.



Jul 9 '06 #7

P: n/a
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/defaul...agemanipulator

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" <Si******@discussions.microsoft.comwrote in message
news:90**********************************@microsof t.com...
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" wrote:
>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
"Sirplaya" <Si******@discussions.microsoft.comwrote in message
news:BA**********************************@microso ft.com...
>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.



Jul 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.