473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Retrieving Images stored in SQL Server 2005

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
7 2036
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alec | last post by:
Newbie question. I have a database for displaying the names of bed and breakfasts searched for by the town they are in as below. <?php $result = @mysql_query ("SELECT name FROM...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
17
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I...
3
by: Susanne Klemm | last post by:
Hello! I use a procedure to insert a new row into a table with an identity column. The procedure has an output parameter which gives me the inserted identity value. This worked well for a long...
0
by: ccorbett | last post by:
Hi, I am pretty new to microsoft programming environment. I want to create forms in visual basic 2005 studio that read data from stored procedures and load into the forms for editing and new...
1
by: charlesg | last post by:
I am seriously i need for a solution to this problem fast.I have just inherited an SQL server DB where the images were stored in the database using a table mandatepictures.The images column is...
5
by: djhexx | last post by:
Hello. I have an ASP.NET application (C#) that I allow users to upload files. Files get stored in a SQL2005 database. The file data is stored in a varbinary(max) column. When the user uploads...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
2
by: pozze | last post by:
Hi, I need to display images and other record information retrieved from an SQL 2005 database in a datagrid on a web page. I'm coding in VB .net I have recently changed over from VB ASP and i'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.