Received the Microsoft Most Valuable Professional award for the year 2011

It’s been three year in a row now. I am MVP for the year 2011 also. It feels so great to get the news that I have been MVP again.

Very big thanks to the MVP Team, My MVP Lead and Microsoft for giving the MVP award to me for 3 year in a row.

Also a great thanks to all the friends, Family, developers, community members that have worked with me. Without your support this would not have been possible. I will try and continue the work in 2011 also

A new vulnerability in asp.net application was found last friday. This vulnerability exists include in all the Asp.net sites, including Asp.Net (All versions), Asp.Net MVC, SharePoint, Any framework in Asp.Net Like Dot Net nuke etc.

An attacker of this vulnerability can use it and download file like web.config which contains sensitive data about the application including connection string for database etc.

Currently a (temporary) workaround (till a proper patch for this vulnerability is made available) has been provided by Microsoft.
The workaround involves setting the customerror in such a way that it returns only one page on any

Continued...

Using Case statement in LINQ to SQL

Working with LINQ I realized that I had to use the simple case statement in my SQL query. There is no special keyword for this. To create a case statement like structure you will have to do it in the select section of the query.

Below is an example of the usage of the case statement in LINQ.

Continued...


Difference in values when converting double to integer using cast or convert

Check out the code below.

double d = 13.6;

int i1 = Convert.ToInt32(d);
int i2 = (int)d;

Continued...


Math.Round Method rounding to nearest even number

What would be the output of the following program?

 Console.Write(Math.Round(-0.5).ToString() + “~”);
 Console.Write(Math.Round(0.5).ToString()+ “~”);
 Console.Write(Math.Round(1.5).ToString()+ “~”);

 Console.Write(Math.Round(2.5).ToString()+ “~”);

Continued...


Exiting the process with the help of Enviroment.FastFail method

Have you ever had some point in code where you want to exit the application immediately? Normally for this purpose you would use the Application.Exit method.  But Microsoft has provided a better method for this purpose which not only just exits the application but also adds a message to the Windows Application event log.

The Enviroment.FastFail method takes a string as a parameter writes that value to the Windows Application Event log, creates a dump of the application and exits the process. The string passed is also included in the error reporting to Microsoft.

FastFail method should be used instead of the Application.Exit method in case the application is damaged // resource Lost etc. beyond damage and

Continued...

Convert string to uppercase when comparing after normalizing instead of lowercase

While doing string comparison after normalizing (converting both string to upper case or lower case) we should always use the upper case. Upper case is optimized for normalization.

 A small group of characters, when they are converted to lowercase, cannot make a round trip. To make a round trip means to convert the characters from one locale to another locale that represents the same character data differently, and then to accurately retrieve the original characters from the converted characters.

Converting to uppercase rather than lowercase can also prevent incorrect behavior in certain cultures. For example, in Turkish, two lowercase i's map to the same uppercase I.

Continued...


Network statistics for upload and download

Last few days while working with my broadband network (downloading few stuff) I wanted to check the exact amount of download per minute. I tried to check the data from task manager and resource monitor. But these gave data in terms of percentage of network use. Hence I could never find the exact amount of download.

So I wrote a small application to sniff the network and find out the exact amount of download. After fetching the data we can do lots of thing like creating speed chart etc…

To fetch the network we use the System.Net.NetworkInformation namespace

First we need to get the network Interface to track. A Machine might be connected to many network and we need to track each network differently. We can do that by the code

Continued...

Community Tech day on Visual Studio 2010 and Asp.net 4.0 on 11 July

We are having community Tech day’s event in Kolkata on 11th of July 2010. Event will be held in Moksh Banquet Hall, Block-A, 22 Camac Street, Kolkata 700016.

Click here to Register for this CTD

Some of the key topics of discussion in the event and the speakers are given below.

Parallel programming with the .NET 4.0Continued...


All the posts in LINQ series

In Last few weeks I have done a few LINQ series Post. Here is a list of all the posts done.


Filtering data in LINQ with the help of where clause

Using Take and skip keyword to filter records in LINQ


Continued...


Applying aggregate function in LINQ

LINQ also provides with itself important aggregate function. Aggregate function are function that are applied over a sequence like and return only one value like Average, count, sum, Maximum etc…
Below are some of the Aggregate functions provided with LINQ and example of their implementation.

Count

    int[] primeFactorsOf300 = { 2, 2, 3, 5, 5 };

    int uniqueFactors = primeFactorsOf300.Distinct().Count();

The below example provided count for only odd number.

Continued...


LINQ and various types of joins

While working with data most of the time we have to work with relation between different lists of data. Many a times we want to fetch data from both the list at once. This requires us to make different kind of joins between the lists of data.

LINQ support different kinds of join

Inner Join

    List<Customer> customers = GetCustomerList();

    List<Supplier> suppliers = GetSupplierList();

 

Continued...


Feature rich release of Visual Studio 2010

Visual studio 2010 has been released to RTM a few days back. This release of Visual studio 2010 comes with a big number of improvements on many fronts. In this post I will try and point out some of the major improvements in Visual Studio 2010.

1)      Visual studio IDE Improvement. Visual studio IDE has been rewritten in WPF. The look and feel of the studio has been improved for improved readability. Start page has been redesigned and template so that anyone can change the start page as they wish.

Continued...


Performance tuning measurement with the help of the StopWatch class

Many of the times while doing the performance tuning of some, class, webpage, component, control etc. we first measure the current time taken in the execution of that code. This helps in understanding the location in code which is actually causing the performance issue and also help in measuring the amount of improvement by making the changes.

This measurement is very important as it helps us understand the problem in code, Helps us to write better code next time (as we have already learnt what kind of improvement can be made with different code) .

Normally developers create 2 objects of the DateTime class. The exact time is collected before and after the code where the performance needs to be measured.  Next the difference between the two objects is used to know about the time spent in the code that is

Continued...

LINQ and the use of Repeat and Range operator

LINQ is also very useful when it comes to generation of range or repetition of data.  We can generate a range of data with the help of the range method.

    var numbers =

        from n in Enumerable.Range(100, 50)

        select new {Number = n, OddEven = n % 2 == 1 ? "odd" : "even"};


The above query will generate 50 records where the record will start

Continued...

LINQ and Element operators to retrieve specific records

While working with data it’s not always required that we fetch all the records. Many a times we only need to fetch the first record, or some records in some index, in the record set. With LINQ we can get the desired record very easily with the help of the provided element operators.

Simple get the first record.

If you want only the first record in record set we can use the first method [Note that this can also be done easily done with the help of the take method by providing the value as one].

    List<Product> products = GetProductList();

Continued...


Using conversion operators in LINQ to convert result set to desired format

LINQ has a habit of returning things as IEnumerable. But we have all been working with so many other format of lists like array ilist, dictionary etc that most of the time after having the result set we want to get them converted to one of our known format. For this reason LINQ has come up with helper method which can convert the result set in the desired format. Below is an example

var sortedDoubles =

        from d in doubles

        orderby d descending

Continued...


Performing Set operation Distinct, Union, Intersect and Except in LINQ

There are many set operation that are required to be performed while working with any kind of data. This can be done very easily with the help of LINQ methods available for this functionality. Below are some of the examples of the set operation with LINQ.

Finding distinct values in the set of data.

We can use the distinct method to find out distinct values in a given list.

    int[] factorsOf300 = { 2, 2, 3, 5, 5 };

    var uniqueFactors = factorsOf300.Distinct();

Continued...


LINQ And Group keyword for grouping of data

While working with any kind of advanced query grouping is a very important factor. Grouping helps in executing special function like sum, max average etc to be performed on certain groups of data inside the date result set. Grouping is done with the help of the Group method. Below is an example of the basic group functionality.

    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

   

    var numberGroups =

Continued...


Using the Order clause to sort the result set

After filtering and retrieving the records most of the time (if not always) we have to sort the record in certain order. The sort order is very important for displaying records or major calculations. In LINQ for sorting data the order keyword is used.

With the help of the order keyword we can decide on the ordering of the result set that is retrieved after the query.  Below is an simple example of the order keyword in LINQ.

    string[] words = { "cherry", "apple", "blueberry" };

    var sortedWords =

Continued...


LINQ Using TakeWhile and SkipWhile to filter records based on condition

In my last post I talked about how to use the take and the Skip keyword to filter out the number of records that we are fetching. But there is only problem with the take and skip statement. The problem lies in the dependency where by the number of records to be fetched has to be passed to it. Many a times the number of records to be fetched is also based on the query itself.

For example if we want to continue fetching records till a certain condition is met on the record set. Let’s say we want to fetch records from the array of number till we get 7. For this kind of query LINQ has exposed the TakeWhile Method.

    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

Continued...


Filtering records with the Skip and take keyword in LINQ

In LINQ we can use the take keyword to filter out the number of records that we want to retrieve from the query. Let’s say we want to retrieve only the first 5 records for the list or array then we can use the following query

    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var first3Numbers = numbers.Take(3);

The TAKE keyword can also be easily applied to list of object in the following way.

var

Continued...

LINQ and where clause to filter out data

LINQ has bought with itself a super power of querying Objects, Database, XML, SharePoint and nearly any other data structure. The power of LINQ lies in the fact that it is managed code that lets you write SQL type code to fetch data. 

Whenever working with data we always need a way to filter out the data based on different condition. In this post we will look at some of the different ways in which we can filter data in LINQ with the help of where clause.

Simple Filter for an array.

Let’s say we have an array of number and we want to filter out data based on some condition. Below is an example

Continued...


Code expression HTML encode in Asp.Net

Hi,


One of the new features in Asp.Net 4.0 is the inclusion of Code expressions which are HTML encoded by default. IN Asp.Net the code expression by default does not encode any text and hence it can leave the chance of Cross Site scripting attack.


In Asp.Net 4.0 we can now write expression which will get encoded by itself. For writing HTML encoded expression we need to use the following expression

 

<%: %>

This could have been easily done in the previous version also by using the HttpUtility.HtmlEncode method in the expression. But it has been made easy now by providing a common expression. Below is an example of same

Continued...

Request Validation now validates All Asp.Net resources

Hi,

When a page is submitted, users can also script along with the post data. Also unauthorized postback could be triggered. The event validation mechanism reduces the risk of unauthorized postback requests and callbacks when the EnableEventValidation property is set to true. This would help and provide default level of protection against cross site scripting.

 

In the previous versions of Asp.Net request validation was turned on by default but the validation would only apply to the asp.net pages (aspx pages and their code behind). This means there is no validation for other files requested like css, image etc.

 

In Asp.Net 4.0 this behavior has been changed by default and now the

Continued...

Single Quotation Marks will be encoded in HtmlEncode and UrlEncode

Hi,

One of the small but important change in the Asp.Net 4.0 is the change is the Encode methods. Now the HtmlEncode and UrlEncode methods in the HttpUtility and HttpServerUtility class respectively also encode the single quote (‘).

The HtmlEncode method encodes instances of the single quotation mark as "&#39;".

The UrlEncode method encodes instances of the single quotation mark as "%27".

Vikram


"Have Breakfast… or…Be Breakfast!"

Who sells the largest number of cameras in India?

Your guess is likely to be Sony, Canon or Nikon. The answer is: None of the above. The winner is Nokia, whose main line of business in India is not cameras but cellphones.

The reason is that cameras bundled with cellphones are outselling standalone cameras. Now, what prevents the cellphone from replacing the camera outright? Nothing at all.

Try this. Who runs the biggest music business in India? The answer is Airtel. By selling caller tunes (that play for 30 seconds) Airtel earns more than music companies do by selling albums.

Airtel is not in the music business. It is the mobile service provider with the largest subscriber base in India. That sort of a competitor is difficult to detect and even more difficult to beat. By the time you have identified him, he has already gone past you. But if you imagine that Nokia and Bharti (Airtel's parent) are breathing easy, you couldn't be further from the truth.

Nokia has

Continued...

Release candidate for Visual Studio 2010 Released

Hi,

Today Microsoft has released Visual Studio 2010 (VS 2010) RC for MSDN Subscriber. It will be available for the General people on RC on 10th February.

If you are an MSDN subscriber you can go ahead and download the bits right now from the link below.

http:////msdn.microsoft.com//en-us//vstudio//dd582936.aspx

The early feedback on Twitter seems very good already about the performance. Do check it out.

 

Update:Support for Silverlight 4 with the VS 2010 RC will show up with next public SL4 drop (and so is not yet enabled with

Continued...

Displaying Multi line wrapped text in web form

Hi,


Many a times there is requirements for us to display text in multi-line in web pages. The first try that most of the people do is by giving a fixed width to the label. But this never works.

Height and Width works only for block level element. Label is an inline element and hence setting width does not work.

 

What you can instead do is use a textbox with CSS that will make it looks like a label. To do this we need to set the following properties.

Wrap = true;
rows = 5 (change this number as per your need.)
ReadOnly = true
TextMode = multiline

Continued...
 
Copyright © 2006 - 2012 Vikram Lakhotia