【问题标题】:Unable to cast object of type 'System.Int32' to type 'System.Byte[]'无法将“System.Int32”类型的对象转换为“System.Byte []”类型
【发布时间】:2019-01-16 21:36:05
【问题描述】:

我将图像保存到数据类型图像(二进制)的 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>

【问题讨论】:

标签: c# asp.net sql


【解决方案1】:

无法将“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();
    }

【讨论】:

    【解决方案2】:

    正如@Alireza 提到的,您似乎正在使用在 DataReader 中找到的第一个值。

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

    这是img 列的正确列号吗?

    如果没有,请尝试将0 值更改为列的正确数字,看看是否再次出现错误。

    【讨论】:

      【解决方案3】:

      我建议您不要使用“SELECT * FROM”,而是将您要使用的列的名称放在那里,例如"SELECT Column1 FROM ..."

      看起来DataReader[0] 是一个 int 值而不是二进制值。

      【讨论】:

        【解决方案4】:

        做:

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

        代替:

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

        【讨论】:

          猜你喜欢
          • 2021-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多