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

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Hi

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">
    <Columns>
        <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" />
    </Columns>
</asp:GridView>

 

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);
    //doc.LoadXml(xml);
    doc.LoadHtml(html);
    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);
    MultiView1.SetActiveView(View1);
    SelectTreeNode(tvBookContent.Nodes, html);
}
Posted 5 months ago
suhaas121 says:
bytecode

 Your bytecode Type is GridView and you are trying to convert GridView to Byte[] which is not possible.

To get the Byte[] you need to us GridView1.SelectedRow.Cells[3].Text

You have to convert the text to Byte[] according to you data.