当我试图检查该行中是否存在该特定图像时,如果我设置了值== null,则显示错误,代码在找到图像时工作正常,但我想在没有图像时不打印截图。所有这些都是在gridview中完成的
protected void gvupdationsummary_RowDataBound(object sender,GridViewRowEventArgs e){
DataRowView dr = (DataRowView)e.Row.DataItem;
if(dr!=null)
{
//string src = Convert.DBNull(imageUrl);
string imageUrl="";
if (imageUrl!=null)
{
Convert.IsDBNull(imageUrl);
imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["imgdata"]);
(e.Row.FindControl("image1") as Image).ImageUrl = imageUrl;
}
else
{
Convert.IsDBNull(imageUrl);
imageUrl = null;
(e.Row.FindControl("image1") as Image).ImageUrl = imageUrl;
}
}
发生异常是因为dr["imgdata"]
包含DBNull.Value
,它无法直接转换为字节数组。你应该在使用演员之前对DBNull.Value
进行检查。
请注意Convert.IsDbNull()
返回bool
值,用它来检查if
内部条件,如下例所示:
if (!Convert.IsDbNull(dr["imgdata"]))
{
imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["imgdata"]);
// do other things
}
else
{
// do something else
}
注意:
Convert.IsDBNull(imageUrl)
是不必要的,因为空字符串应该用string.IsNullOrEmpty()
而不是Convert.IsDbNull()
来检查。