Varbinary

Discussion in 'ASP .Net' started by gsauns, Sep 12, 2007.

  1. gsauns

    gsauns 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?
    gsauns, Sep 12, 2007
    #1
    1. Advertising

  2. On Sep 12, 7:55 pm, gsauns <> wrote:
    > 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=...
    Alexey Smirnov, Sep 12, 2007
    #2
    1. Advertising

  3. gsauns

    gsauns 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.
    gsauns, Sep 12, 2007
    #3
  4. On Sep 12, 8:45 pm, gsauns <> wrote:
    > 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
    Alexey Smirnov, Sep 12, 2007
    #4
  5. gsauns

    gsauns Guest

    On Sep 12, 2:59 pm, Alexey Smirnov <> wrote:
    > On Sep 12, 8:45 pm, gsauns <> wrote:
    >
    > > 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


    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.
    gsauns, Sep 12, 2007
    #5
  6. How do you upload / download your images from your SQL database?
    That could be where your problem is.

    Try this article http://aspnet.4guysfromrolla.com/articles/120606-1.aspx
    for a very good method to store and display binary files.

    Let me know how you get on.

    Hope this helps.

    James
    =?Utf-8?B?SmFtZXMgUGFnZQ==?=, Sep 13, 2007
    #6
  7. On Sep 12, 9:29 pm, gsauns <> wrote:
    > On Sep 12, 2:59 pm, Alexey Smirnov <> wrote:
    >
    >
    >
    >
    >
    > > On Sep 12, 8:45 pm, gsauns <> wrote:

    >
    > > > 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

    >
    > 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
    Alexey Smirnov, Sep 13, 2007
    #7
  8. gsauns

    gsauns Guest

    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);
    }
    }
    }
    gsauns, Sep 13, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinod
    Replies:
    9
    Views:
    21,192
    nivs1978
    May 20, 2010
  2. =?Utf-8?B?TmFtZXNwYWNl?=

    Convert varbinary SQLtype to 1-dimensional array of bytes

    =?Utf-8?B?TmFtZXNwYWNl?=, Mar 13, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    2,374
    =?Utf-8?B?TmFtZXNwYWNl?=
    Mar 13, 2005
  3. js
    Replies:
    3
    Views:
    963
    Andrea Desole
    Oct 17, 2005
  4. =?Utf-8?B?cm9kY2hhcg==?=

    store a null in varbinary

    =?Utf-8?B?cm9kY2hhcg==?=, Mar 8, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    568
    =?Utf-8?B?cm9kY2hhcg==?=
    Mar 9, 2007
  5. GaryDean

    Display .DOC file in Varbinary

    GaryDean, Aug 13, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    883
    Walter Wang [MSFT]
    Aug 15, 2007
Loading...

Share This Page