472,800 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 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 1993
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.