Monday, 17 June 2013

Error 349: Duplicate headers received from server in Google Chrome while rendering PDF using ITextSharp in Asp.Net MVC



While working on generating  PDF in Asp.Net MVC3, I got a peculiar error while rendering the PDF in Chrome browsers, which is not being found in any other browsers like Internet Explorer or Fire Fox.
This error comes because chrome browsers doesn't ignore duplicate headers. This can be addressed easily the following ways, if you or your web app is sending out headers. Then check the following things:

 1. Enclose fileName using “”. i.e
Response.Addheader('Content-Disposition: attachment; filename="' + fileName '"');
instead of
Response.Addheader('Content-Disposition: attachment; filename='+ fileName);
2. If possible replace spaces and commas (,) with underscores (_)
 string regExp = @"[^\w\d]";
Response.AddHeader("content-disposition""attachment;filename=" + "Test_PDF_" + 
                           Regex.Replace(model.CompanyName, regExp, "_")+ "_" +
                              Regex.Replace(model.FullName, regExp, "_") + "_" +                                                         DateTime.Now.ToShortDateString());teString());

3. Explicitly tell Asp.Net MVC to override headers by setting optional replace parameter to true.
Response.AddHeader("Content-type: application/pdf",true);
Happy Coding!

Friday, 14 June 2013

How to prevent the multiple submit of record in ASP.Net MVC using JQuery

There are various methods are available to do that one, it can be done using JQuery, using sessions or Filters.


Method -1 ( Using JQuery Approach)
 $('form').submit(function () {
        if ($(this).find('.input-validation-error').length == 0) {
            $(this).find(':submit').attr('disabled''disabled');
        }
    });

This is the simplest method to prevent the multiple submission of the record in MVC and even it works on JQuery Unobstructive validations.

Happy Coding!

Wednesday, 5 June 2013

How to get a set of record from middle of the table, Say you need records from 5 to 15 from a table containing 100 records in SQL Server

Its a small query that will help you to find the records from from  n to m from a table having N records.

Where N :- total records in the table.
            n :- starting record number.
            m :- ending record number.

select top 20 * from School
except
select top (5) * from School

so, here it first select top 20 records and from it it discards the 1st top 5 records.

Enjoy!

Writing multiple condition for a Kendo Grid column Template to fetch the record.

Sometime you are required to write a template for Kendo Grid to fetch the data on a condition below is the column template to write multiple condition to do it. Its easy one, but to remember those small tricky things, I had posted this one.



Column[
{ field: "DateActivated", title: "Activated", width: 50, 
template: '#=(DateActivated==null)?"":(Status=="Requested")?"":
 kendo.toString(kendo.parseDate(DateActivated"),"MM/dd/yyyy")#',filterable: false }]



Happy Coding!

Tuesday, 4 June 2013

Getting the Error "The document has no pages" in PDF reporting Using ItextSharp in Asp.net MVC3/MVC4

Today while working with a project, I got a task to do i.e for creating dynamic PDFs of the View.
So I had started working on that and completed before time.

However , when I tried to put into the azure production environment, i got peculiar error which i had not observed in my dev Environment or in the stage.

The error I got is  "The document has no pages" . I started doing research into it but unable to find any solution. At last, I thought that it might be out of memory or buffering issue, no still that is not the case. the problem arises due to inability to find the relative source path that you had provided to the image source in the view while creating the PDF with the logo or images in it.

So, before deploying to the production server be sure the following things has been checked:

1) The Image file which u have used must be existing in the website inside some folder.
2) Be sure that PDF supports few file formats such as .jpg/.bmp, so use the image of that type.
3) Don't use the image source with relative URL, it will not be able to find the image. It will work in the local but throw an error in production or staging server. So better use the live site followed with the image source.


If you are still getting the same error then just try to render some text and check whether it is rendering or not. If it is rendering then there might be some image rendering issue. Is still getting the error then just recheck your code.


Happy coding!