无法将类型为“System.Int32”的对象转换为类型“System.Byte[]”

3

我将图像保存到数据类型为image(Binary)的SQL数据库中,保存成功。现在我需要在我的数据列表中检索它。当我将其绑定到我的标记中的数据列表时,出现以下错误:

无法将类型为'System.Int32'的对象强制转换为类型'System.Byte[]'。

以下是我的后端代码:

 if (!(this.IsPostBack))
    {
        // For News DataList
        prepareConnection();
        _command.CommandText = "select top 5 * from News ORDER BY id DESC";

        _adp = new SqlDataAdapter();
        _tbl = new System.Data.DataTable();
        _adp.SelectCommand = _command;
        _adp.Fill(_tbl);
        SqlDataReader DataReader = _command.ExecuteReader();
        DataReader.Read();
        Response.BinaryWrite((byte[])DataReader[0]);
        DataReader.Close();
        dlNews.DataSource = _tbl;
        dlNews.DataBind();
    }


  protected void prepareConnection()
{
    _connection = new SqlConnection(@"Data Source=******\localhost;Initial Catalog=******;User ID=sa;Password=****");
    _connection.Open();
    _command = new SqlCommand();
    _command.Connection = _connection;

}

这里是DataList的标记:

<asp:DataList ID="dlNews" runat="server">
                    <ItemTemplate>
                        <a href='./NewsView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;">
                            <div id="123">
                                <a href='NewsView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;">
                                    <asp:Label ID="lblTitle" runat="server" Style="font-size: 15px; font-weight: bold;
                                        line-height: 20px;" Text='<%# Eval("Title").ToString().Length>70 ? (Eval("Title") as string).Substring(0,70) : Eval("Title") %>'></asp:Label>
                                </a>
                                <div id="image" style="clear: both; float: right; margin: 0 5px 10px 10px;">
                                    <a href='NewsView.aspx?ID=<%#Eval("img") %>' style="text-decoration: none;">
                                        <asp:Image ID="Image1" runat="server" Height="111px" ImageUrl="~/images/epica.jpg"
                                            Style="border: 1px solid black;" />
                                    </a>
                                </div>
                                <div style="margin-removed 20px; padding-removed 5px;">
                                    <asp:Label ID="lblContent" runat="server" Text='<%# Eval("Contect").ToString().Length>150 ? (Eval("Contect") as string).Substring(0,150) : Eval("Contect") %>'></asp:Label>
                                </div>
                                <div style="position: relative; border: 1px solid black; float: left; background-color: #4EAAF5;
                                    margin-left: 3px;">
                                    <a href='NewsView.aspx?ID=<%#Eval("ID") %>' style="color: white; font-size: 15px;
                                        font-weight: bold; text-decoration: none; padding: 4px;">إقرأ المزيد</a>
                                </div>
                            </div>
                            <div id="Separator" style="width: 600px; height: 2px; border-top: 1px solid #CCCCCC;
                                margin: 5px 17px 5px 5px; clear: both;">
                            </div>
                        </a>
                    </ItemTemplate>
                </asp:DataList>

1
你在表格“新闻”中的第一列不是类似于“Id”吗? - Alireza
请查看以下链接:https://dev59.com/EXM_5IYBdhLWcg3wlEJH - Vikash Singh
4个回答

2

无法将类型为'System.Int32'的对象强制转换为类型'System.Byte[]'。

根据您的错误信息,您的Response.BinaryWrite方法接收到的是Int32类型的输入,而该方法无法进行转换。正如其他用户所回答的那样,在选择时使用列名,DataReader[0] 是您的第一列值,我认为它是ID并且它是Int32类型,因此请在查询中使用列名,并使用正确的列名获取正确的列值(DataReader[2] 这将按照下面的查询为您获取图像列)。

 if (!(this.IsPostBack))
    {
        // For News DataList
        prepareConnection();
        _command.CommandText = "select top (5) ID ,Title,img,Contect from News ORDER BY id DESC";

        _adp = new SqlDataAdapter();
        _tbl = new System.Data.DataTable();
        _adp.SelectCommand = _command;
        _adp.Fill(_tbl);
        SqlDataReader DataReader = _command.ExecuteReader();
        DataReader.Read();
        Response.BinaryWrite((byte[])DataReader[2]);
        DataReader.Close();
        dlNews.DataSource = _tbl;
        dlNews.DataBind();
    }

0
我建议你不要使用"SELECT * FROM",而是在那里放入你想要使用的列名,例如"SELECT Column1 FROM ...."
看起来DataReader[0]是一个整数值而不是二进制值。

0
正如@Alireza所提到的,您似乎正在使用DataReader中找到的第一个值。
Response.BinaryWrite((byte[])DataReader[0]);

这是列的正确列号吗?

如果不是,请尝试将0的值更改为该列的正确编号,然后查看是否再次出现错误。


-1

做:

Response.BinaryWrite(DataReader[0] as byte[]);

不是:

Response.BinaryWrite((byte[])DataReader[0]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接