如何在网格上使用处理程序显示图像,如果在另一条记录上图像为NULL则传递异常。 [英] How can I make the handler dispaly images when availble on the grid, and pass exception if image was NULL on someother record.

查看:57
本文介绍了如何在网格上使用处理程序显示图像,如果在另一条记录上图像为NULL则传递异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库(名称,电子邮件,电子邮件,图像),其中某些记录中的图像为NULL

我制作了一个图像处理程序来在gridview上显示图像。

我得到一个例外



I have a database with (name,phonenumber,email, image) where the image in some records are NULL
I made an imagehandler to display images on the gridview.
I am getting an exception

using (con)
{
    con.Open();
    SqlDataReader dReader = cmd.ExecuteReader();
    dReader.Read();
 
    //// Unable to cast object of type 'System.DBNull' to type 'System.Byte[]
    context.Response.BinaryWrite((byte[])dReader["Photo"]);
    //// Unable to cast object of type 'System.DBNull' to type 'System.Byte[]
 
    dReader.Close();
}

推荐答案

在使用之前检查字段是否为空

Check the field isn't null before you use it
if (!dReader.IsDBNull(dReader.GetOrdinal("Photo")))
{
    context.Response.BinaryWrite((byte[])dReader["Photo"]);
}
else
{
    // do something else
}


这篇关于如何在网格上使用处理程序显示图像,如果在另一条记录上图像为NULL则传递异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆