Varbinary

G

gsauns

Not sure if this is belongs here, or maybe in SQL...

I have a SQL Server 2005 database where I store a varbinary(MAX)
field. In my Web form, I allow the user to upload a photo to the
database. It's stored in the varbinary field.

I used an HTTP handler to display the image in an HTML image control
on a FormView. I have a button that allows the user to delete the
photo from the database.
This button runs "UPDATE table SET photo=NULL" on the database. That
works fine, the field is set to NULL in the database. However,
whenever I select the record in the FormView from which I deleted the
photo, the html Image control is still the same size as it was when
the photo was in there. The photo is gone, and it gives the "alt"
text, but the control is still big. It persists even when I exit and
re-enter my app. Is there something I have to do to the control, or
the database field, to shrink it back down to original size?
 
G

Guest

Not sure if this is belongs here, or maybe in SQL...

I have a SQL Server 2005 database where I store a varbinary(MAX)
field. In my Web form, I allow the user to upload a photo to the
database. It's stored in the varbinary field.

I used an HTTP handler to display the image in an HTML image control
on a FormView. I have a button that allows the user to delete the
photo from the database.
This button runs "UPDATE table SET photo=NULL" on the database. That
works fine, the field is set to NULL in the database. However,
whenever I select the record in the FormView from which I deleted the
photo, the html Image control is still the same size as it was when
the photo was in there. The photo is gone, and it gives the "alt"
text, but the control is still big. It persists even when I exit and
re-enter my app. Is there something I have to do to the control, or
the database field, to shrink it back down to original size?

Do you use width/height there?
<asp:Image width=... height=...
 
G

gsauns

No, I am not using width/height.
It is not an ASP image control, it's the HTML control. I couldn't
acheive the same functionality with the ASP control, since i am
filling in the image control with context.Response.OutputStream.Write
in the handler. Running the HTML control server-side doesn't work
either, as it won't display the image. So I can't manipulate it thru
code.

When I go to a new record (that doesn't contain a photo), the image
control is sized just fine. But going back to the same record, it gets
big again.
 
G

Guest

No, I am not using width/height.
It is not an ASP image control, it's the HTML control. I couldn't
acheive the same functionality with the ASP control, since i am
filling in the image control with context.Response.OutputStream.Write
in the handler. Running the HTML control server-side doesn't work
either, as it won't display the image. So I can't manipulate it thru
code.

When I go to a new record (that doesn't contain a photo), the image
control is sized just fine. But going back to the same record, it gets
big again.

hm, it sounds strange, maybe it is something with the cache? Try to
clean it or try to see if Firefox has the same behavior
 
G

gsauns

hm, it sounds strange, maybe it is something with the cache? Try to
clean it or try to see if Firefox has the same behavior

Clearing the cache doesn't do anything.
Firefox does NOT have the same behavior, because IE displays the small
"X" along with the image frame and the alt text, where Firefox just
displays the alt text... could be an IE quirk? Installing Firefox on
all clients' machines is not a viable solution for me, I wish I could
just figure it out. Thanks for your help.
 
G

Guest

Clearing the cache doesn't do anything.
Firefox does NOT have the same behavior, because IE displays the small
"X" along with the image frame and the alt text, where Firefox just
displays the alt text... could be an IE quirk? Installing Firefox on
all clients' machines is not a viable solution for me, I wish I could
just figure it out. Thanks for your help.- Hide quoted text -

- Show quoted text -

I think it would be good if you can give us an example of your code.
I'll try to check it. Thanks
 
G

gsauns

Thanks guys. See anything here?

Here's the image control in the FormView:
<img id="imgPhoto" alt="No photo." src='ImageHandler.ashx?contactID=<
%# Eval("contact_id") %>' />

Here is uploading the binary data:
public void formviewContacts_OnInserting(object sender,
SqlDataSourceCommandEventArgs e)
{
int intLen =
((FileUpload)formviewContacts.FindControl("fileImage")).PostedFile.ContentLength;
byte[] bytImage = new byte[intLen];
if
(((FileUpload)formviewContacts.FindControl("fileImage")).HasFile)
{
Response.Write(bytImage.Length.ToString());

((FileUpload)formviewContacts.FindControl("fileImage")).PostedFile.InputStream.Read(bytImage,
0, intLen);
SqlParameter param = new SqlParameter("@image",
SqlDbType.VarBinary);
param.Direction = ParameterDirection.InputOutput;
param.Value = bytImage;
e.Command.Parameters.Add(param);
}
}

....And here is my Handler class:
<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.IO;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class ImageHandler : IHttpHandler {
public bool IsReusable {
get {
return true;
}
}
public static Stream GetPhoto2(int photoid)
{
using (SqlConnection connection = new
SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("SELECT [Photo]
FROM [Contact] WHERE [contact_id]=@id", connection))
{
command.Parameters.Add(new SqlParameter("@id", photoid));
connection.Open();
object result = command.ExecuteScalar();
try
{
return new MemoryStream((byte[])result);
}
catch
{
return null;
}
}
}
}
public void ProcessRequest (HttpContext context)
{
//Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// Setup the PhotoID Parameter
Int32 id = -1;
Stream stream = null;
id = Convert.ToInt32(context.Request.QueryString["contactID"]);
stream = GetPhoto2(id);
const int buffersize = 1024 * 16;
byte[] buffer2 = new byte[buffersize];
int count = stream.Read(buffer2, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer2, 0, count);
count = stream.Read(buffer2, 0, buffersize);
}
}
}
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top