473,836 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2065
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******@discu ssions.microsof t.comwrote in message
news:BA******** *************** ***********@mic rosoft.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******@discu ssions.microsof t.comwrote in message
news:BA******** *************** ***********@mic rosoft.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="multip art/form-data"
method="post">
<a href='#'><asp:I mage ID="imgProdItem " runat="server" /></a>
</form>
</body>
</html>

code-behind:

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

string sImgQuery = "SELECT img_data, img_name, img_contenttype " +
"FROM tbl_FeatureBoxe s " +
"WHERE CategoryID = " + strCatID + " AND " +
"SubCategor yID = " + strSubID + " AND " +
"BoxName = 'Online'";

oCmd.CommandTex t = sImgQuery;
oCmd.CommandTyp e = CommandType.Tex t;
oCmd.Connection = oConn;

oConn.Open();
oDr = oCmd.ExecuteRea der();
if (oDr.Read())
{
string img_data = oDr["img_data"].ToString();
if (img_data == "")
{
this.imgProdIte m.ImageUrl = "~/images/img_niu.gif";
}
else
{
byte[] byteArray = (byte[])oDr["img_data"];
Response.Conten tType = oDr["img_contenttyp e"].ToString();
Response.Binary Write(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******@discu ssions.microsof t.comwrote in message
news:BA******** *************** ***********@mic rosoft.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******@discu ssions.microsof t.comwrote in message
news:BA******** *************** ***********@mic rosoft.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******@discu ssions.microsof t.comwrote in message
news:BA******** *************** ***********@mic rosoft.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******@discu ssions.microsof t.comwrote in message
news:8F******** *************** ***********@mic rosoft.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******@discu ssions.microsof t.comwrote in message
news:BA******* *************** ************@mi crosoft.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.a spx 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.asp x 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******@discu ssions.microsof t.comwrote in message
news:90******** *************** ***********@mic rosoft.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="multip art/form-data"
method="post">
<a href='#'><asp:I mage ID="imgProdItem " runat="server" /></a>
</form>
</body>
</html>

code-behind:

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

string sImgQuery = "SELECT img_data, img_name, img_contenttype " +
"FROM tbl_FeatureBoxe s " +
"WHERE CategoryID = " + strCatID + " AND " +
"SubCategor yID = " + strSubID + " AND " +
"BoxName = 'Online'";

oCmd.CommandTex t = sImgQuery;
oCmd.CommandTyp e = CommandType.Tex t;
oCmd.Connection = oConn;

oConn.Open();
oDr = oCmd.ExecuteRea der();
if (oDr.Read())
{
string img_data = oDr["img_data"].ToString();
if (img_data == "")
{
this.imgProdIte m.ImageUrl = "~/images/img_niu.gif";
}
else
{
byte[] byteArray = (byte[])oDr["img_data"];
Response.Conten tType = oDr["img_contenttyp e"].ToString();
Response.Binary Write(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******@discu ssions.microsof t.comwrote in message
news:BA******* *************** ************@mi crosoft.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
1571
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 site01_details WHERE town='$townsearch' AND code='sleep'"); while ($row = mysql_fetch_array($result))
3
3494
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. -Regards Arun
17
4900
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 can retrieve the autonum value for the new record. This doesn't occur with SQL Server, which of course causes an error (or at least in this code it does since there's an unhandled NULL value). Is there any way to retrieve this value when I add a...
3
2852
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 time. Now the identity value is over 700.000 and I get errors whiles retrieving the inserted identitiy value. If I delete rows and reset the identity everything works well again. So I think it is a data type problem. My Procedure:
0
2321
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 insertions back into the database. I have created new projects using datasets that are filled by three stored procedures that are bound to the forms using a datasource. The problem is, we may be using Oracle as well as SQL Server to retrieve this...
1
1565
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 called pictures and the data type is binary.From information i have gathered from this forum and the problems i see on ground, i have decided that the way to go is to store this images on the filesystem and refernce the path on a databse table field. The...
5
2331
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 the file, I store it in a database. When a user requests to download a file, the file is retrieved from the database and then sent to them. The files go in there ok. This is how I store them:
0
3400
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 within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
2
2802
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 still getting used to .net I can successfully upload images and other data into the SQL Database, I'm currently storing RecordID, File Name, Type of File (mime), File Size, and File Data. I can retreive non binary data to a datagrid but I cannot...
0
9812
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10532
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.