Sunday, 12 January 2014

How to Add Re-ordering of the columns in Telerik MVC or Razor Grid.

Some times it is required that the column  had to be re-ordered in a grid as, we are using a Telerik grid , as adding the property reorder will do the task.


Here is the code below to do it:

 .Reorderable(reorder => reorder.Columns(true))

  Just add this line to your Telerik Razor grid, it will do the task.


Happy Coding!

How to show the Pager in Telerik Grid both in top and bottom.

While working in a project , my client wants to show the paging in the grid in both in the top of the grid as well as in the bottom of the grid also. As I am using the Telerik grid, it has a property to set while creating the grid. 

So here is the code for it:


.Pageable(p =>
      {
         p.Style(GridPagerStyles.NextPreviousAndNumeric | GridPagerStyles.PageSizeDropDown);    
         p.Position(GridPagerPosition.Both); // this line will do the Magic
         p.PageSize(int.Parse(ViewBag.PageSize ?? "50"), new[] { 10, 20, 50 });
       }).Sortable()
Happy Coding!

Monday, 23 September 2013

How to Insert space before capital letters using Jquery

Assume you have a string "HelloWorld" and you need to show as "Hello World". How you can achieve it using Jquery.

It can be done in a very simpler way as below:

"Your string".replace( /([a-z])([A-Z])/g, "$1 $2")

so here it will be "HelloWorld".replace( /([a-z])([A-Z])/g, "$1 $2") will produce "Hello World"

Enjoy!

Wednesday, 17 July 2013

Differentiate between 'All' and 'Any' Operators in LINQ.

In LINQ, All operator is  to check whether all the elements met the specific condition or not. While Any operator checks whether there is any element exist in the collection or not.


For Example:

int array={1,2,3,4,5};

bool result=array.All(i=>i < 6); here it will check the elements and returns true.

Similarly,

bool result= array.Any( ); // here it checks whether the collection has any elements and returns true.


Hope you Enjoyed!

Monday, 15 July 2013

Compile Time View Checking In Asp.net MVC

You might have noticed that in MVC3/MVC4 if you had done some thing wrong in the view files it will not throw any error during compilation and it will build successfully. The error in the file is only noticed during executing/rendering the view file. So in order to trap the error during the compilation time you can follow the method below:

Step-1 :
Open your solution project file in any XML Editor or in the Visual Studio itself  and you will find a tag named
<MVCBuildViews>, here it false and just make it true and save it.

<MVCBuildViews>false</MVCBuildViews>  
 change it to 
<MVCBuildViews>true</MVCBuildViews>


Thats all, so now when your view has any error then it will throw error during the compilation.


The Use of ALL Operator in LINQ

Today, I came across the All operator in LINQ. This operator returns bool value rather than records after condition is satisfied. The operator will be highly helpful when you need some kind of validation to find unique data. for e.g. Checking the existing email  or UserID registered and so on.


Lets Go with a Example to do the Task:

int [] array = { 1,2,3,4,5 };

bool result = array.All(value => value > 2 );

Console.WriteLine(result);  // Here it will print False since it will check all the values and                                         // all the values must be grater than 2.


 result = array.All(value => value < 6 );
Console.WriteLine(result); // Here it will print True since it will check all the values and                                          // found all values are less than 6.


Hope it helps you!

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!