Showing posts from January, 2012

Useful overloads for String.Split()

Most of C# developers are familiar with this String.split() method. I have found some useful overloads for split method of the string class split the string in array of string.

Example 1:

String.Split( char[])

string words = "string1,string2,string3,,string4 string5.";
string [] split = words.Split(new Char [] {' ', ','});

Above code create a string array which has


but What If I want to remove empty string from the array when I split string.
Solution to this problem is to make use of second overload method of the the string Split where you can specify the option to remove string. So above code is rewritten as

Overload method with option
String.Split(Char[], StringSplitOptions)

string words = "string1,string2,string3,,string4 string5.";
string [] split = words.Split(new Char [] {' ', ','},StringSplitOptions.RemoveEmptyEnt…

RequiredField Validator not working with CKEditor in ASP.Net

Last month I had worked on a project where we were using CKEDITOR. For validation using required field validator. It is working for all control but unfortunately required field validator is not working for CKEDITOR.

After searching I have got the reason and solution for this.


The requiredfield validator control will not work properly when it is used with a textarea that is configured with CKEditor. This is due to the fact that, the CKEditor content will not be synched to the page element(textarea) properly when the validator control fire.


To overcome this difficulty, we need to call the method updateElement() in order to sync or post the content back to the textarea. The below code does that,

<script src="_scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src=&qu…

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."

Very recently I have worked on MVC application. Everything is working fine for MVC v1. But when move to MVC v2 I have gotten this error-

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."

After making some r & d I have found the solution:


In MVC v2 they block Json for GET requests (as you can tell from the error) for security reasons.

If your JSON payload:

1.Contains sensitive non-public data
2.Returns an Array
3.Responds to a GET request
4.Browser making the request has JavaScript enabled (very likely the case)
5.Browser making the request supports the __defineSetter__ method.

Then the data is vulnerable to a JSON hijacking. Typically, it's not *your* data but the data of the users of your website.

For more details about JSON Hijacking.

1.A possible solution I found online…

Stopwatch class to determine the execution time for an application

A Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

If you want to measure the time required for your programs written in the C# language. This could be for a micro-benchmark or for routine and continuous performance monitoring.


First sample:
In this sample you must create Stopwatch as an instance. This makes it useful in multithreaded applications or websites.

using System;
using System.Diagnostics;
using System.Threading;
class Program
static void Main(string[] args)
Stopwatch stopWatch = new Stopwatch();
Thread.Sleep(10000);//or do some work
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpa…

SQL Server case/when type conversion problem

In my last project I wrote a query where I have to sort data based on the user input but each column in table has different data type. So to meet with this type of requirement I have used case..when block of the sql server.

Problem: Sample query

DECLARE @sortby Varchar(10)
Set @sortby = 'A1'

Select String1,String2,DateCol
from (
select 'A1','B1', GetDateTime()
select 'A2','B2', GetDateTime()
) As d(String1,String2,DateCol)
Order by
When @sortby = 'A1' then String2
When @sortby = 'B1' then String1
When @sortby = 'Date' then Date1

there is not syntax error when you check for the syntax error but when execute code you find there is error

ERROR:Conversion failed when converting datetime from character string.

the problem here is first two having databype Varchar and last one having datatype DateTime. So when you executing its found that one branch having datetime so its gives error even its not executing tha…

Html stripper (Remove Html tag)

This is a common problem when we want to show any data in gridview( or table or repeater it breaks the page or design of the page due to html tag inside the presented date.

It will also cause the problem if we want use CKEDITOR to insert data. Because when we insert anything using rich editor it is wrap the text with <p></p>. If we want to show partial of the string it will cause page break because last </p> is missing.

So to avoid this problem we need to strip the Html tag from the string. We can do it in various ways such as regular expression and character array.

Solution 1(Good):

using System.Text.RegularExpressions;
//Remove HTML from string with Regex.
public static string StripTagsRegex(string source)
return Regex.Replace(source, "<.*?>", string.Empty);

Solution 2(Better):

using System.Text.RegularExpressions;
// Compiled regular expression for performance.
static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Com…

Fixed problem for continuous string(long string without space) using regular expression

Last day I have faced a problem for a long string like(adfdsfsdfdsfdsfsdafdsfsdfsdfsdfsdfsdfsdfsdfsdffdsfsdfsdfsdafsdfsdfsdfsdfsdfsdafsdf) which make my application's UI design break. So to fixed this issue I have used regular expression which insert a space after n characters.

To insert a space after every n characters:

var newString = Regex.Replace(oldString, new string('.', n), m => m.Value + " ");

Sample solution:
string longWord = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaassssssssssssssssssssssssdddddddddddddddddddddddddddddaaaaaaaaaafffffffffffffffffffffffffadfdsfsdfdsfdsfsdafdsfsdfsdfsdfsdfsdfsdfsdfsdffdsfsdfsdfsdafsdfsdfsdfsdfsdfsdafsdffdsfdsfdsfsdf";

Regex.Replace(longWord, new string('.', 16), m => m.Value + " ");

In above image it shows that regex place a space after every 16 character for that long string.

JavaScript parseInt() bug

Very recently I have faced a strange problem with JavaScript parseInt() function. It returns incorrect result when try to parse value like "08","09"...... But it works for "01" to "07".
So I have tried to find solution in community. Then find that this was a JavaScript bug.


There is a "bug" with the parseInt JavaScript function. The bug is not something that will affect you very often, but it is something you should be aware of. We've seen the bug in every browser except Opera.

I've created a button to demonstrate the bug. The bug is that parseInt can return an incorrect value. For example, parseInt("08") results in 0 instead of 8. And parseInt("09") results in 0 instead of 9. The reason for this is because the zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers. The button below builds statements from pa…