Hello Everyone
I need your help displaying images on a web page. I have the following code
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:
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
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;
}
}
}
HTML:
context.Response.BinaryWrite((byte[])dReader["Image"]);
[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