checking binary data for IMAGE field

  • I'm a asp.net developer. I'm using Gridview and some httphandlers to show Image data in project. The table description is as follows

    Table name : ImagesStore

    column : ImageId (primary key ,auto increment) int,

    ImageData Binary field (to store jpeg,jpg,gif etc images)

    Gridview calls the ThumbNailImage (IHttpHandler) to make thumbnil in gridview itself , on clicking image it's calling FullImage (IHttpHandler) to view in a new window.

    the code is as follows

    using System;

    using System.Web;

    using System.Data.SqlClient;

    using System.Configuration;

    public class FullImage : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

    string imageid = context.Request.QueryString["ImID"];

    if (imageid == null || imageid == "")

    {

    //Set a default imageID

    imageid = "1";

    }

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AddressBook"].ConnectionString);

    connection.Open();

    SqlCommand command = new SqlCommand("select ImageData from ImagesStore where ImageId=" + imageid, connection);

    SqlDataReader dr = command.ExecuteReader();

    dr.Read();

    context.Response.BinaryWrite((Byte[])dr[0]);

    connection.Close();

    context.Response.End();

    }

    public bool IsReusable {

    get {

    return false;

    }

    }

    }

    The problem arises when we have absolutely no data in ImageData field which is a binary field, the exception is thrown.

    Now all I want is to modify such that SQL query to make sure if ImageData is empty or not when ImageId is passed along with it. Now if it's empty we will not select it else we will show in grid view.

    Please suggest alternative method for that.

    Thanks in advance.

  • Hi

    The SQL query is not the problem. You have to determine if any data have been returned and if the data are not DBNull.

    // ...

    byte[] resp = null;

    // Determine if any data have been returned

    if (dr.Read())

    {

    object data = dr[0];

    // Determine if the returned date are not DBNull

    if (!(data is DBNull))

    {

    resp = (byte[])data;

    }

    }

    context.Response.BinaryWrite(resp);

    // ...

    Greets

    Flo

  • If you do not want to return anything if ImageData is empty you can add simple checking "is not null"

    SqlCommand command = new SqlCommand(String.Format("select ImageData from ImagesStore where ImageId={0} and ImageData is not null", imageid), connection);

    But in case the ImageData is empty, will get an exception on row "context.Response.BinaryWrite((Byte[])dr[0]);"

    you will need to add a checking on dr.Read().

    if(dr.Read())

    {

    context.Response.BinaryWrite((Byte[])dr[0]);

    }

    -------------------------
    - Name?
    - Abu Dalah Sarafi.
    - Sex?
    - 3 times a week!
    - No, no. Male or Female?
    - Male, female, sometimes camel...

  • Thanks for conceptual help ,i had tried to implement this & worked also

    code is

    using System;

    using System.Web;

    using System.Data.SqlClient;

    using System.Configuration;

    public class FullImage : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

    string imageid = context.Request.QueryString["ImID"];

    if (imageid == null || imageid == "")

    {

    //Set a default imageID

    imageid = "1";

    }

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AddressBook"].ConnectionString);

    connection.Open();

    SqlCommand command = new SqlCommand("select ImageData from ImagesStore where ImageId=" + imageid, connection);

    SqlDataReader dr = command.ExecuteReader();

    dr.Read();

    try

    {

    context.Response.BinaryWrite((Byte[])dr[0]);

    }

    catch (ArgumentOutOfRangeException e)

    {

    dr.Dispose();

    SqlCommand command1 = new SqlCommand("select ImageData from ImagesStore where ImageId =4", connection);

    SqlDataReader dr1 = command1.ExecuteReader();

    dr1.Read();

    context.Response.BinaryWrite((Byte[])dr1[0]);

    }

    connection.Close();

    context.Response.End();

    }

    public bool IsReusable {

    get {

    return false;

    }

    }

    }

    Thanks

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply