ASP.Net Error: Unable to cast object of type System.Web.UI.WebControls.GridView to type System.Byte[]

Last Reply 7 months ago By dharmendr

Posted 7 months ago


I want to read binary file on click from gridview.

I get an error like this : 

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Byte[]'.

Filepath in the below gridview is binary file stored in DB.

<asp:GridView ID="GridView1" CssClass="grid" runat="server" AutoGenerateColumns="false"
    OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged"
    AllowPaging="true" OnPageIndexChanging="OnPaging" PageSize="20">
        <asp:BoundField ItemStyle-Width="150px" DataField="vcModuleTitle" HeaderText="Module" />
        <asp:BoundField ItemStyle-Width="150px" DataField="vcUserName" HeaderText="User Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="dHistoryDateTime" HeaderText="Date Time" />
        <asp:BoundField ItemStyle-Width="150px" DataField="vcFilePath" HeaderText="File Path" />


protected void OnSelectedIndexChanged(object bytecode, EventArgs e)
    byte[] bytes = null;
    bytes = (byte[])bytecode;
    //XmlDocument doc = new XmlDocument();
    var doc = new HtmlDocument();
    string html = Encoding.UTF8.GetString(bytes);
    html = GridView1.SelectedRow.Cells[3].Text;

    Page page = (Page)HttpContext.Current.Handler;
    MultiView control = ((Helicopter_IETM.DataCollection)(page)).MultiView1;
    View View1 = (View)control.FindControl("View1");
    HtmlControl ctrl = (HtmlControl)View1.FindControl("frmDisplay");
    ctrl.Attributes.Add("src", HttpContext.Current.Request.Url + "/.." + html);
    SelectTreeNode(tvBookContent.Nodes, html);
This question does not have replies that have been liked.