Click here to Skip to main content
14,393,196 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am getting the above error i tried to bind List and all and result gives me some other error, like HTMLWorkHelper and Paragraph converter error etc.
and i am using Itextsharp Version 5.4.1,
and while i google it says update to new version.

What I have tried:

protected void ExportToPDF(object sender, EventArgs e)
       {
           Response.ContentType = "application/pdf";
           Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           StringWriter sw = new StringWriter();
           HtmlTextWriter hw = new HtmlTextWriter(sw);
           PanelHistoryNew.RenderControl(hw);
           StringReader sr = new StringReader(sw.ToString());
           Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
           HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
           PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
           pdfDoc.Open();
           htmlparser.Parse(sr);
           pdfDoc.Close();
           Response.Write(pdfDoc);
           Response.End();
       }
Posted
Updated 29-May-19 5:52am
Comments
Richard MacCutchan 29-May-19 7:54am
   
Most likely because they are not based on the same object type.
Gerry Schmitz 29-May-19 9:58am
   
No error checking. No checking of results. You are beyond help.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can only cast between "related" types, or between types which have a cast operator defined (using either the explicit or implicit keywords : Conversion operators - C# Programming Guide | Microsoft Docs[^]

Think about it: if you declare three classes:
public class Fruit {...}
public class Apple : Fruit {...}
public class Orange : Fruit {...}
Then you can cast an Apple to a Fruit, or an Orange to a Fruit, or a Fruit to either an Apple or an Orange (but you may get a run time error if you try) - but you can't cast a Orange to an Apple or vice versa (and that's why you could get the run time error I mentioned) because an Apple contains information that an Orange doesn't: a RemoveCore method method perhaps. And if you could cast it, then when you tried to call RemoveCore it would fail at runtime because it can't find the method.

In essence, you can cast a derived class to any of the classes it is derived from (or interfaces it implements), and you can compile while casting from a base class to any class derived from it (or from an interface to a class that implements it ) - but you may fail at run time if the actual object instance isn't compatible.

To do any other casting, you need to tell the system exactly what can be casted by proving explicit or implicit operators to actually do it.

And that is what you error message is saying: "this object cannot be converted to this type" so use the compiler (if that generates the error) or debugger (if your code compiles), and look at the actual line that gives the error to find out what data you are trying to convert or pass incorrectly.

We can't do that for you!
   
Comments
Raghav Panalkar 3-Jun-19 6:09am
   
Thank you for explanation.
OriginalGriff 3-Jun-19 6:22am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100