Question Getting an error while trying to load an image

MrBlack

New member
Joined
Oct 10, 2012
Messages
2
Programming Experience
Beginner
Hello Everyone

I need your help displaying images on a web page. I have the following code
HTML:
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
              
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        // Create SQL Command 

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ImageName,Image from aspnet_cesar where ID =@ID";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["Image"]);
       
        dReader.Close();
        con.Close();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}
It works just fine if i have a value under the Image column, but if i have a null value in the Image column my codes breaks under this line:
HTML:
context.Response.BinaryWrite((byte[])dReader["Image"]);
I am getting the following error message:
[HTML"Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'."}][/HTML]

"Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'."} Can you please help me on how to correct this problem?

Thank you so much in advance

BK
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,124
Location
Sydney, Australia
Programming Experience
10+
I thought that I'd already answered this question. Maybe it was at another forum.

Anyway, you are getting data from a nullable column so you have to allow for that fact. You can't simply cast an object as a byte array if it might not be a byte array, and DBNull.Value, which is what's used to represent database nulls, is not a byte array. Maybe you're assuming that it would just be a C# null, but it's not. As I said in my previous response to this question, the data reader has its own method to test whether a field is null so you need to call that first to see if the field is null and only use its value if it's not. It's really common sense, isn't it? Check to see if there's a value and then use it if and only if there is.
 

MrBlack

New member
Joined
Oct 10, 2012
Messages
2
Programming Experience
Beginner
Thnak you so much for your responce. I'll give it a try. :)
 

annaharris

Member
Joined
May 8, 2013
Messages
13
Programming Experience
1-3
I used the code and rectified it as you said, but still the image is not loading. Thanks anyways.
 
Top Bottom