简体   繁体   中英

Unable to cast object of type 'System.Int32' to type 'System.Byte[]'

I saved image into sql data base of datat type image(Binary) and its successfully saved .. now i need to retrive it in my datalist .. after i bound it into my datalist in my markup .. i got this error

Unable to cast object of type 'System.Int32' to type 'System.Byte[]'.

here is my behind code

 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;

}

and here is DataList markup :

<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>

Unable to cast object of type 'System.Int32' to type 'System.Byte[]'.

As your error already saying that your Response.BinaryWrite method is receiving Int32 type as input which this method is unable to Cast, As above answers by other users, Use column names in query while selecting and DataReader[0] is your first column value which i think is ID and it's Int32 , So use column name in query as below and use correct column name to fetch correct column value ( DataReader[2] this will fetch you your image column as per below query).

 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();
    }

As @Alireza mentioned you appear to be using the first value found in the DataReader.

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

Is this the correct column number for the img column?

If not try changing the 0 value to the correct number for the column and see if you get the error again.

I suggest you don't use "SELECT * FROM", instead put the names of the columns you want to use in there instead eg "SELECT Column1 FROM ...."

It looks like DataReader[0] is an int value not a binary value.

Do:

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

Instead of:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM