Thursday, June 19, 2008

Couldn’t find ConfigurationManager in System.Configuration?

I am trying to pull an appSettings key from my web.config. In .NET 1.1, we usually use:


However, in .NET2.0, using this code will cause the following error:

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'
So I just change my code to the following, according to the error above. But the problem is I can’t find ConfigurationManager in the System.Configuration.

The reason is because the original System.Configuration.ConfigurationSettings class is point to the System.dll assembly. There is another new the System.Configuration.dll assembly with the new ConfigurationManager class. In this case, you need to add in the System.Configuration.dll as a reference to your application before you can use it.

Hope this will help someone.

Wednesday, June 18, 2008

Export GridView to PDF

Recently I have been trying to generate some reports to PDF file format from ASP. NET2.0 application. There are a lot of open source PDF libraries out there that you can use to export to PDF such as iTextSharp, Gios PDF .NET Library and PDFSharp. You can go to this link to find out more open source PDF libraries in C#.

Later I will show you a working solution on how to export GridView to PDF by using one of the free libraries – iTextSharp.

ITextSharp is a port of the iText open source java library written entirely in C# for the .NET platform. It is a library that allows developers to extend the capabilities of their web server applications with dynamic PDF document generation and generate PDF file on the fly.

Before that, you need to download the iTextSharp library. Here is the download link.

Add in the iTextSharp.dll as a reference into your web application.

Here is my sample of code:

using iTextSharp.text;
using iTextSharp.text.pdf;

protected void Page_Load(object sender, EventArgs e)

private void ExportToPDF()
  Document document = new Document(PageSize.A4, 0, 0, 50, 50);
  System.IO.MemoryStream msReport = new System.IO.MemoryStream();

  try {
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);

    // we add some meta information to the document
    document.AddSubject("Export to PDF");


    iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);

    datatable.Padding = 2;
    datatable.Spacing = 0;

    float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
    datatable.Widths = headerwidths;

    // the first cell spans 7 columns
    Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Leading = 30;
    cell.Colspan = 7;
    cell.Border = Rectangle.NO_BORDER;
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);

    // These cells span 2 rows
    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultHorizontalAlignment = 1;
    datatable.DefaultRowspan = 2;
    datatable.AddCell(new Phrase("Full Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL)));
    datatable.AddCell("Telephone No.");

    // This cell spans the remaining 3 columns in 1 row
    datatable.DefaultRowspan = 1;
    datatable.DefaultColspan = 3;
    datatable.AddCell("Just Put Anything");

    // These cells span 1 row and 1 column
    datatable.DefaultColspan = 1;
    datatable.AddCell("Col 1");
    datatable.AddCell("Col 2");
    datatable.AddCell("Col 3");

    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultRowspan = 1;

    for (int i = 1; i < 20; i++) {
      datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
      datatable.AddCell("This is my name.");
      datatable.AddCell("I have a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long address.");

      datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;

  catch (Exception e) {

  // we close the document

  Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
  Response.ContentType = "application/pdf";

Hope these codes can help those people who are new to developing and save some time on their searching solutions. You can also find the tutorial of iTextSharp at here.

Happy Programming!

Monday, June 16, 2008

Error “There was an error opening this document. The file is damaged and could not be repaired”

Few days ago, I was trying to export report in Excel, Word and PDF format.

The code that I used works perfect for the Excel and Word file, however, when I tried to do the same on PDF file, I got the following error in Adobe Acrobat Reader version 7 and Internet Explorer 6.0:

There was an error opening this document. The file is damaged and could not be repaired.
I had been searched the solution for hours. I had tried various other things such as Response.BinaryWrite, Response.ClearContent, Response.ClearHeaders, and also some other header settings, but no luck.

Finally, I got this code that works fine.

Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.ContentType = "application/pdf";

Hope this can hope those facing the same problem. Cheers!

Monday, June 9, 2008

Difference between String and string

If you notice in your codes, String, which typed, with an uppercase S is colored in light blue. If it is typed in the lowercase string, it is colored in dark blue. What is the difference between String and string actually?

Nothing really, string is a C# language keyword, which is an alias for System.String in .NET framework. So do int = System.Int32, bool = System.Boolean and short = System.Int16. The only subtle difference is that string can be used without a using System; directive, while String needs it, otherwise you will need to specify System.String in full.

Hope this can provides you a clearer understanding about string and String.

Wednesday, June 4, 2008

JavaScript not firing with UpdatePanel

Yesterday when I am dealt with textbox inside an UpdatePanel, I noticed that the JavaScript is not fire when the TextChanged handler is triggered.

I had the code lines as below to fire my JavaScript function.

string strScript = "javascript:alert('Testing')";
ClientScript.RegisterStartupScript(typeof(Page),"clientScript", strScript);

After working around with the bug, finally I found the cause. The ClientScript.RegisterStartupScript is just cannot working within an UpdatePanel. To work around the problem there are related static methods in the class ScriptManager that should be used when the control is used inside an UpdatePanel, as the following:

string strScript = "javascript: alert('Testing')";
ScriptManager.RegisterStartupScript(this.gridPanel, this.GetType(), "strScript", strScript, true);

In short, when you are using UpdatePanel in your form, don’t use ClientScript.RegisterStartupScript. More information please refers to:

Hope this can help you anyway. Cheers.

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template