-1

I have redImage and whiteImage in my database table colourGame. Their datatype is image. And also, binary image. I want to display them in a gridview.

SqlDataAdapter da= new SqlDataAdapter("SELECT 
                                         ballID,
                                         gameLevel,
                                         gameChapter,
                                         redImage,
                                         whiteImage 
                                      from 
                                         colourGame 
                                       WHERE 
                                         gameLevel = '"+ ddlLevel.Text+"' 
                                         AND gameChapter = '"+ddlChapter.Text+"' ", cn);

ddlLevel and ddlChapter are dropdownlist.

ddlLevel.Items.Add("3");
        ddlLevel.Items.Add("4");


        ddlChapter.Items.Add("1");
        ddlChapter.Items.Add("2");
        ddlChapter.Items.Add("3");
        ddlChapter.Items.Add("4");
        ddlChapter.Items.Add("5");

In my database table data, if redImage have image, whiteImage will be NULL and vice versa.

Therefore I experienced an error regarding null value.

4

3 回答 3

0

你应该试试 IsNULL()。

SqlDataAdapter da= new SqlDataAdapter("SELECT 
                                     ballID,
                                     gameLevel,
                                     gameChapter,
                                     IsNULL(redImage,'')[redImage],
                                     IsNULL(whiteImage,'')[whiteImage] 
                                  from 
                                     colourGame 
                                   WHERE 
                                     gameLevel = '"+ ddlLevel.Text+"' 
                                     AND gameChapter = '"+ddlChapter.Text+"' ", cn);
于 2013-10-03T08:37:27.793 回答
0

编辑:

你大概可以这样写:

<asp:Image ID="imgWhite" Url='<%# Eval("Image") == DBNull.Value ? "" : Eval("Image") %>'></asp:Image>

首先,阅读SQL INjections

在 Rowdatabound 上,您必须将该图像和下拉菜单绑定到 gridview,然后只有它会显示。

例如。

    protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    DropDownList ddl = (DropDownList)gvUserInfo.FindControl("yourControlName");
    string ddlValue = ddl.SelectedItem.Value;
    TextBox txtUserInfo = (TextBox) gvUserInfo.FindControl("yourControlName");
    string strValue = txtUserInfo.Text;
    Image imgCtrl = (Image) args.Row.FindControl("imgCtrl");
    imgCtrl.ImageUrl = m_ConcatUrl;
    }

以同样的方式,你将不得不绑定图像。

于 2013-10-03T07:42:34.087 回答
0

如果您只处理数据库中的 null 并且只返回有效图像怎么办?

注意查询中的合并。这将只返回第一个非空值。

编辑:(合并到一个空字符串而不是第二个图像)

SqlDataAdapter da= new SqlDataAdapter("SELECT 
                                     ballID,
                                     gameLevel,
                                     gameChapter,
                                     coalesce(redImage,'') as [redImage],
                                     coalesce(whiteImage, '') as [whiteImage]
                                  from 
                                     colourGame 
                                   WHERE 
                                     gameLevel = '"+ ddlLevel.Text+"' 
                                     AND gameChapter = '"+ddlChapter.Text+"' ", cn);

关于SQL注入的评论是真实的,你需要参数化这个查询

编辑#2:处理您的错误。这是未经测试的,但img要测试它是否为 DBNull。

return new MemoryStream((byte[])img);

改成:

If(img.GetType() == typeof(System.DBNull)) return new MemoryStream();
return new MemoryStream((byte[])img);
于 2013-10-03T07:48:35.990 回答