haripriyamca05 0 Newbie Poster

I have gridview for file download with linkbutton which keeps filename as commandargument and one more column with number of files to be downloaded.

Condition is that if have more than one file i redirect to another page otherwise i can download my file there itself.

I am having my download codings in rowcommand and there i have to check with number of files cell value to redirect or to download. how can i get that cell value in rowcommand.

Note: I calculate and bind the number of files field in rowdatabound at runtime.

My design page is:

<asp:GridView ID="grv_files" runat="server" AutoGenerateColumns="False" Width="90%" DataKeyNames="fld_fid"
    OnRowCommand="grv_files_RowCommand" AllowPaging="True" OnPageIndexChanging="grv_files_PageIndexChanging" OnRowDataBound="grv_files_RowDataBound">
        <Columns>
            <asp:BoundField DataField="rno" HeaderText="Sno" />
            <asp:BoundField DataField="fld_cname" HeaderText="Customer Name and City" />
            <asp:BoundField DataField="fld_podate" HeaderText="PO Date" HtmlEncode="False" DataFormatString="{0:dd/MM/yyyy}" />
            <asp:BoundField DataField="fld_povalue" HeaderText="PO Value" />            
            <asp:TemplateField HeaderText="Download">
                <ItemTemplate>                    
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("fld_filename") %>' CommandName="cmd">Download</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>    
            <asp:BoundField HeaderText="No Of Files" />        
        </Columns>
    </asp:GridView>

My Code Page is:

protected void grv_files_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lnk = (LinkButton)e.Row.Cells[5].FindControl("LinkButton1");
            if (lnk != null)
            {
                string filename = lnk.CommandArgument;
                int occ=filename.IndexOf(',');
                if (occ > -1)
                {
                    string[] arrfiles = filename.Split(',');
                    e.Row.Cells[6].Text = arrfiles.Length.ToString();
                }
                else
                {
                    e.Row.Cells[6].Text = "1";
                }
            }
        }
    }

 protected void grv_files_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmd")
        {
           [B] GridViewRow grv = grv_files.SelectedRow;

            string fileno =grv.Cells[5].Text;
[/B]
            if (fileno == "1")
            {
                string filename = e.CommandArgument.ToString();
                string path = MapPath("~/PO/" + filename);
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                if (file.Exists)
                {
                    byte[] bts = System.IO.File.ReadAllBytes(path);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Type", "Application/octet-stream");
                    Response.AddHeader("Content-Length", bts.Length.ToString());
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                    Response.BinaryWrite(bts);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "javascript", "<script>alert('This file was not Found/ Deleted /Moved to other directory');</script>");
                    return;
                }
            }
            else
            {
                string strScript = "<script> ";
                strScript += "var newWindow = window.open('download.aspx', '_blank','height=450, center:yes, width=600, status=no, resizable= yes, menubar=no, toolbar=no, location=yes, scrollbars=no, status=no');";
                strScript += "window.location='download.aspx';";
                strScript += "</script>";
                ClientScript.RegisterClientScriptBlock(GetType(),"strScript", strScript); 
            }
        }
    }

In the above rowcommand i get object reference error from where i am getting that 5th cell value [bolded line].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.