Showing posts from April, 2012

File Upload in ASP.NET MVC3

If you are a web developer you often need to upload file on the web server or database. In today’s post I am going explain how we can upload file in ASP.NET MVC 3 with razor syntax.

So, first thing we need to create a view that will contain the file upload control. I am going to use an existing mvc template for this demo. So I have just modified the default Index view like following.

Difference between SkipWhile and Where in linq

Before some time I have written a blog post about the SkipWhile operator and a reader of my blog asked me that we can do the same thing with Where also but there is a difference between this two. So In this post I am going to explain you difference between those two.

Where operator filters a list of collection based on condition. It will filter whole collection while SkipWhile will only skip those elements in list until condition is true and after that it will stop filtering of skipping.Now let’s take an example where we can see the difference between Where and SkipWhile. Following is a code for that.

Tuple in C# 4.0

C# 4.0 language includes a new feature called Tuple. Tuple provides us a way of grouping elements of different data type. That enables us to use it a lots places at practical world like we can store a coordinates of graphs etc.

In C# 4.0 we can create Tuple with Create method. This Create method offer 8 overload like following. So you can group maximum 8 data types with a Tuple. Followings are overloads of a data type.
Create(T1)- Which represents a tuple of size 1 Create(T1,T2)- Which represents a tuple of size 2 Create(T1,T2,T3) – Which represents a tuple of size 3 Create(T1,T2,T3,T4) – Which represents a tuple of size 4 Create(T1,T2,T3,T4,T5) – Which represents a tuple of size 5 Create(T1,T2,T3,T4,T5,T6) – Which represents a tuple of size 6 Create(T1,T2,T3,T4,T5,T6,T7) – Which represents a tuple of size 7 Create(T1,T2,T3,T4,T5,T6,T7,T8) – Which represents a tuple of size 8

TakeWhile operator in linq

In this post I am going to explain TakeWhile Operator in details. TakeWhile is one of partitioning operator available in Linq. It will take sequence until condition becomes false.

Here is the example for that.

using System; using System.Collections.Generic; using System.Linq; namespace Linq { class Program { static void Main(string[] args) { int[] numbers ={10,9,8,7,6,5,4,3,2,1}; foreach(var number in numbers.TakeWhile(n=>n>5)) { Console.WriteLine(number); } } } }

SkipWhile Method in Linq

I have been playing around linq and I have found great method call SkipWhile method. SkipWhile methods skips particular element which matches condition in predicate this can be use full in condition where we need to skip elements on particular condition.

So let’s take some example. I have written following console application code.

using System; using System.Collections.Generic; using System.Linq; namespace Linq { class Program { static void Main(string[] args) { string[] names = { "Jalpesh", "Jayesh", "Tushar", "Tejas", "Sanjay", "Nijesh" }; foreach(var name in names.SkipWhile(s=>s.ToLower().StartsWith("j"))) { Console.WriteLine(name); } } } }

Full screen in Visual studio 2010

Visual studio is a great IDE and I am learning everyday something new and I have found one great feature today which is available from the Visual studio 2008. So I thought it would great to be share with all you guys.

We all like to have most portion of screen to cover code part so we can view all the lines and we don’t have to use page and page down keys to navigate through code. You can have visual studio in full screen mode with View->Full Screen or via pressing Shift + Alter + Enter as shortcut..

Multicast delegates in c#

In yesterday’s post We learn about Delegates and how we can use delegates in C#. In today’s blog post we are going to learn about Multicast delegates.
What is Multicast Delegates?As we all know we can assign methods as object to delegate and later on we can call that method with the help delegates. We can also assign more then methods to delegates that is called Multicast delegates. It’s provide functionality to execute more then method at a time. It’s maintain delegates as invocation list (linked list).

Delegates in c#

I have used delegates in my programming since C# 2.0. But I have seen there are lots of confusion going on with delegates so I have decided to blog about it. In this blog I will explain about delegate basics and use of delegates in C#.
What is delegate?We can say a delegate is a type safe function pointer which holds methods reference in object. As per MSDN it's a type that references to a method. So you can assign more than one methods to delegates with same parameter and same return type.

Dynamically creating meta tags in mvc

As we all know that Meta tag has very important roles in Search engine optimization and if we want to have out site listed with good ranking on search engines then we have to put meta tags. Before some time I have blogged about dynamically creating meta tags in 2.0/3.5 sites, in this blog post I am going to explain how we can create a meta tag dynamically very easily.

To have meta tag dynamically we have to create a meta tag on server-side. So I have created a method like following.

Creating dynamic breadcrumb in mvc with mvcsitemap provider

I have done lots breadcrumb kind of things in normal web forms I was looking for same for mvc. After searching on internet I have found one great nuget package for mvpsite map provider which can be easily implemented via site map provider. So let’s check how its works. I have create a new MVC 3 web application called breadcrumb and now I am adding a reference of site map provider via nuget package like following.

Execute TSQL statement with ExecuteStoreQuery in entity framework 4.0

I was playing with entity framework in recent days and I was searching something that how we can execute TSQL statement in entity framework. And I have found one great way to do that with entity framework ‘ExecuteStoreQuery’ method. It’s executes a TSQL statement against data source given enity framework context and returns strongly typed result.

You can find more information about ExcuteStoreQuery from following link.

Ternary operator in VB.NET

We all know about Ternary operator in C#.NET. I am a big fan of ternary operator and I like to use it instead of using IF..Else. Those who don’t know about ternary operator please go through below link.

Here you can see ternary operator returns one of the two values based on the condition. See following example.

How to pad number with leading zero with C#

Recently I was working with a project where I was in need to format a number in such a way which can apply leading zero for particular format. So after doing such R and D I have found a great way to apply this leading zero format.

I was having need that I need to pad number in 5 digit format. So following is a table in which format I need my leading zero format.

1-> 00001

So in the above example you can see that 1 will become 00001 and 20 will become 00200 format so on. So to display an integer value in decimal format I have applied interger.Tostring(String) method where I have passed “Dn” as the value of the format parameter, where n represents the minimum length of the string. So if we pass 5 it will have padding up to 5 digits.