0

Export selected (checked) GridView Rows to PDF in ASP.Net Could not find a part of the image path how to solve this error ti shows error again and again but it works without image how to convert selected gridview deatails to pdf? getting error? Below code shows this error

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="624px" CssClass="grid" OnRowCommand="OnRowCommand"
        AllowPaging="True" AllowSorting="True" BackColor="White"  OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
        BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize = "5" 
        OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">          
            <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
        <asp:CheckBox ID="chkHeader" runat="server" onclick="CheckAll(this)"/>
            </HeaderTemplate>
            <ItemTemplate>
            <asp:CheckBox ID="chkchild" runat="server" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="updatedby" HeaderText="updatedby" SortExpression="updatedby" />
            <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
            <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
            <asp:BoundField DataField="mail" HeaderText="mail" SortExpression="mail" />
            <asp:BoundField DataField="imagename" HeaderText="imagename" SortExpression="imagename" />  
            <asp:ImageField DataImageUrlField="uploadimage" HeaderText="uploadimage" ControlStyle-Width = "60" ControlStyle-Height = "100">                
            </asp:ImageField>              
            <asp:CommandField ShowEditButton="True" />                
                <asp:TemplateField HeaderText="Delete" >
                    <ItemTemplate>                  
               <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/delete.jpg"
                   OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete" CommandName="Delete" />                              
                    </ItemTemplate>   
               </asp:TemplateField>
                <asp:TemplateField HeaderText="Update">
                    <ItemTemplate>
                       <asp:LinkButton ID="link" runat="server" Text="Choose" CssClass="linkbutton" CommandName="Choose"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Print">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td><asp:ImageButton ID="excel" runat="server" ImageUrl="images/excel-icon.png" OnClick="excel_Click" /></td>
                                <td> <asp:ImageButton ID="pdf" runat="server" ImageUrl="images/PDF-icon.png" OnClick="pdf_Click1"/></td>
                                <td><asp:ImageButton ID="word" runat="server" ImageUrl="images/MS-Office-2003-Word-icon.png" OnClick="word_Click"/></td>
                            </tr>
                        </table>        
                      </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Report">
                    <ItemTemplate>
                        <asp:Button ID="Button5" runat="server" Text="Get details" BackColor="#33CCFF" CssClass="getdetails" OnClick="Button5_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>
        </Columns>
</asp:GridView>

C# code

       using (StringWriter sw = new StringWriter())
       {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
         GridView1.Columns[0].Visible = false;   
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {`
                    row.Visible = (row.FindControl("chkchild") as CheckBox).Checked;   
                }
            }
            GridView1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }
Mari
  • 237
  • 1
  • 4
  • 17
  • Duplicated question. Try this solutions http://stackoverflow.com/questions/9611535/itextsharp-parsing-html-with-images-in-it-it-parses-correctly-but-wont-show-ima – Pavel Timoshenko Nov 14 '14 at 12:49

1 Answers1

0

You forgot tilde "~" in ImageUrls, check and change your code in your ImageButtons

<asp:ImageButton ID="excel" runat="server" ImageUrl="~/images/excel-icon.png" OnClick="excel_Click" />
G.Anıl Yalçın
  • 188
  • 1
  • 14