C# 4.0 introduces a new keyword called 'Dynamic'. It can consume any object anything. Let's see some examples for that.

dynamic intExample = 1;Response.Write(intExample);

dynamic floatExample = 2.5;Response.Write(floatExample);

dynamic stringExample = "DotNetJaps";Response.Write(stringExample);

It will print out put on web page as following.

Now, you will have question what's new in that. It could be also done with var keyword . Yes, you can do same thing with var but dynamic keyword is slightly different then var keyword.

Diffrence between var and dynamic keyword:

var keyword will know the value assigned to it at compile time while dynamic keyword will resolve value assigned to it at run time. I know you guys don't believe me without example. So let's take example of string.

Now try to compile above code it will not compile and it gives a error that 'string' does not contain a definition for 'MethodDoesnotExist' and no extension method 'MethodDoesnotExist' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?'. So var keyword knows that what value or object or anything assigned to it. Now lets try to compile same code with dynamic example like following.

This will compile. So it is the difference between dynamic and var keyword. With dynamic keyword anything assigned to it like property,objects operators anything that will be determined at compile time. It can be useful while we are doing programming with com,JavaScript which can have runtime properties.

Today one of my friend asked me about simple insert,update and delete example with LINQ-To-SQL but at that time i was not having any simple example which will show the power of LINQ-To-SQL Example. So i have decided to create one simple example with LINQ-To-SQL and also decide to blog about it. Let's create a simple example which is very simple and also show basic step to use LINQ-To-SQL. First i have created a simple table called which have 4 fields like ProductId,Name,Description and Price. Like following.

Here is the description for each fields

ProductId- A id which uniquely identify each product.

Name- Name of Product

Description- A short description about product

Price- Price of Product.

Now, Lets create a Linq-To-SQL and rename it as MyDataContent.dbml as following. For that Go to your project ->Right Click->Add New Item->Go to Data Template-> LINQ to SQL Classes like following.

Then open Database Explorer and drag and drop Product table on the newly generated LINQ-to-SQL Class like following.

Now our LINQ-SQL-Class is Ready. Now we will insert some data to our table and then first we will write code to get all product information from the database. I have created a function called GetProduct which will print all the product information on page with the help of Response.Write . Following is code for that.

ASP.NET 4.0 having many many new features. One of them is the RepeatLayout property for the CheckBoxList and RadioButtonList controls. The property is useful when we need decide how this controls will be render as html when its load in browser .

C# 4.0 has one new cool features which is the named parameter. Suppose you have so many parameter in function and when you call them its hard to remember the sequence of the code now with the named parameter you can have name of the parameter with value like Parameter:value and one another cool feature of the name parameter is the you don't need to pass the parameter in exact sequence. Let see the following example

Code Snippet

protectedvoid Page_Load(object sender, EventArgs e)

{

foreach (int i in Square( max:10,min:1))

{

Response.Write(i.ToString() + " ");

}

}

publicstaticIEnumerable<int> Square(int min, int max)

{

for (int i = min; i < max; i++)

{

yieldreturn i*i;

}

}

In above example I am having a function called square with two parameter min and max and having a function which will return the square of the given range between min and max. See the for each loop carefully. I have named parameters and another thing you will notice that i have changed the sequence of parameter but still its working fine. So this is a very cool new feature from the C# 4.0.

I am loving more and more visual studio 2010 for some of the cool things. One of the new feature i have found in visual studio 2010 is the selected text. In the earlier version of visual studio 2010 when we select tax then its not maintaining the highlighted code but with visual studio 2010 it is maintain the colored syntax. Isn't that cool?. We can still know that what we are selecting. Like following.

Yield statement is one of most interesting statement in the C# 2.0. I have heard about lot of yield statement but today i have learned about yield statement. Yield statement can be used to return multiple object from a method while retaining its state. You can get item in sequence with help of yield.Let take a simple example to see the power or yield statement.

Let's create one example which will help you the understand how its works Let create a function will return the square each time this function is called.

Code Snippet

publicstaticIEnumerable<int> Square(int min, int max)

{

for (int i = min; i < max; i++)

{

yieldreturn i*i;

}

}

Now each time this method is called it will return the square of current value within a given range and its also maintains the state between calls. Let create for each loop to call the the square function above.

Code Snippet

foreach (int i in Square(1, 10))

{

Response.Write(i.ToString() + " ");

}

And here will be the output in the browser.

So it will create IEnumerable without implementing any interface and lesser code.

With .net framework 4.0 and C# 4.0 also introduces a new feature called optional parameters it was there in VB since visual basic 6.0. But it is now available in the C# with 4.0 version. Optional parameter will be become very handy when you don't want to pass some parameter and just you need the default value. For that Till C# 3.0 we need to create overloaded functions where we creating function with different argument with same function name. Let's see how its works

Let's go through a simple scenario we will create function with simple response.write which will print a string which will be optional argument.

See the above example in that we have created one created once function PrintMessage which will print message on web page thorough Response.Write. And in the Page_Load event we called two times first one is which without argument which will print default message and another one which will have message an message argument which will print message which we have passed. Lets run that application and following out put will produced.

So that's it it will work like this if you passed argument the it will use argument or otherwise its will use the default argument. This will become handy and you don't need to write overloaded function with default value.

Those who are working with asp.net will also know about the publishing site to directly with publish dialog. This was introduced with visual studio 2005 and till now it is one of the best way to publish your asp.net site with only required files. Visual studio 2010 is not a exception but the publish dialog is enhanced with more feature. Now the its directly given in the toolbar of visual studio 2010 like following.

Once you select the new the following dialog will appear.

In visual studio 2010 publish setting dialog has more options then earlier version. Now you can also save publish profile also and this profile will appear in the publish setting and by choosing that you can directly publish your site from the publish button with same setting you set for this profile.

The publish setting dialog also has more options then earlier version of visual studio. It is having four option.

MSDeploy Publish

Ftp

FileSystem

FPSE

MSDeploy Publish:

With this option you can directly deploy your site on Remote server IIS with MSDeploy tool .This is one of coolest thing in click once deployment. It can also deploy all the IIS setting, content of the website,registry setting for website,required assembly etc. For more details about this please visit following link:

This is the same as visual studio 2008. You can directly publish your site on remote server with ftp protocol. If you have the username and password for ftp protocol then it is the easiest thing to publish your site on remote server.

File System:

This will create a published version of your webaplication on folder which you specified and then you can copy it whatever location you want. If you are having VPN of remote server then you can directly copy the publish version of you web application directly on specified location

FPSE:

FPSE means front page server extension.This is the oldest one which were introduced with the Internet was fairly young, to support things like hit counters, email components. Some developers that develop with FrontPage use these. It also provide way to publish content.

As i am using visual studio 2010 more and more i loving it more. While working with new Microsoft New Visual Studio 2010 Express edition I have came across about two more cool features zooming feature and another the highlighting start and end tag of particular selected tag.

Let see one by one. First feature is the new zooming feature of visual studio 2010 now you can zoom your html and server side code up to 20% to 400%. This feature is very useful when you are teaching some one some thing with your code. Another thing you can also make visible your lots of code in one screen while you set it less then 100%. There is a dropdown given in both source view of html code from which you can zoom it up to 400% like as following.

So you can zoom your code and view in the bigger way like following.

Same way you can do it for the server side code also.

Another feature i have came across is the highlighting of start and end tag in html source view. So user can have idea where the tag can be finish. As you can see in following picture I have selected link button and its highlighting both start and end tag of link button.

We are having a very large project in asp.net 3.5. During code review of project we have found that page_load event of one control is firing twice in the firefox browser. After doing some debugging i have found that it's due to one of the link button which is there in the control. I like to share you the same scenario with you all guys.

One of the developer from us has specified the link button click event two times. The first one in directly in the html with onclick like following.

And another in the initialize event he has also attached the event handler for the click event like following.

Code Snippet

protectedoverridevoid OnInit(EventArgs e)

{

lnkButton.Click += newEventHandler(lnkButton_Click);

base.OnInit(e);

}

So i have removed one of them from initialize event and now its calling page_load event only one time. This is also responsible for the failed to load ViewState error some time when you are adding controls dynamically.

We all are using update panel for the Ajax implementation in every site. Nowadays Ajax is almost there in each and every site. Update panel is working fine with the all other controls but when we use the file upload control then its not working fine. I have searched from lots for this and i have found one of simple solution from the Imran's blog. It is so simple and works very well. You just need to add the "multipart/form-data" attribute to the form.

Microsoft visual studio express editions are light weight IDE provided free by Microsoft and also having almost all the functionality of its big brother Microsoft Visual Studio Professional edition except few. My pc is bit old now Its P4 and its having only 1gigs of ram so the express edition are the best suited for pc like mine. Microsoft has recently provided the beta 2 version of Microsoft new generation IDE called Visual Studio Twenty Ten(2010).

There are lots of changes from visual studio 2008 express edition and Microsoft Visual Studio 2010 Express edition. The first changes you will notice is the brand new splash screen and brand new colored logo for visual studio.

Another thing you will notice is the look and fill of IDE. It all looks like blue every where. Its is best suited for the Microsoft forthcoming operating system Microsoft Windows 7.

Another the difference is the create new project window which is bit stylish and having more options compare the to the visual studio 2008. There are two new things in new project dialog the first one the windows azure tools which is Microsoft's new steps towards the could computing(I will blog about this later one). and another thing is the Silver light application option. Now you can create three kind of application with the Microsoft SilverLight Application

Silverlight Application

Silverlight Navigation Application

Silverlight class library

Another difference is the default font for the IDE earlier it was Courier New and now its Consolas which is the next generation programming font. Which is more visual than the other fonts.

Another major difference is when you create a asp.net web application then its now having more default folders compare to visual studio 2008. There are site.master is also there and some default style sheet just like asp.net mvc applications. You will have default login control like asp.net mvc application. There are two more folder scripts and styles. In style you will have default style sheet for the application. Another folder is for the scripts which are having default JQuery as per earlier announcement Microsoft and JQuery will go together.

Another new things in Microsoft Visual Studio 2010 Visual Web Developer Expression edition is the Tool Setting menu which are having three setting by default.

Basic Settings

Code Optimized

Expert Settings.

All the three option will have different setting for solution explorer and all other stuff. Another new thing is visual web developer 2010 express edition having multi monitor support which is not there in earlier express editions.Visual web developer express also having windows presentation foundation code editor.

Another cool feature is the maximized windows in design view now you will have whole window is maximized in design view hiding other windows. So you visually look all the control more better. Another cool thing is the percentage dropdown through which you can set all screen by percentage as per your requirement.

There are lots of more features like Improved css compatibility,HTML and Javascript snippets,support for asp.net mvc application and support for multi target etc. I will blog about each feature in much more detail. T

If you still not downloaded the future version of visual studio express edition 2010 then you can download it from the following link.

The first session was from vinod kumar on Microsoft Windows 7 and Microsoft Office 2010. Though he is sql server guy and very well known for his site. www.extremeexperts.com he has decided to take session for Microsoft Windows 7 and Microsoft Office 2010. The session was full of information and we have seen some of advance features of Microsoft upcoming operating system Windows 7 and new version of Microsoft Office 2010.

After that we went for the lunch and then followed by two sql server sessions from the Pinal Dave and Jascob Sebastian. Pinal Dave has taken session on SQL server Index and it was very useful session for any person related with software development. All the SQL Server index features is presented and he explains that where we should implement index and where we should not implement index for the better performance of queries through simples examples.

Then we have back to back session from Jacob Sebastian on the SQL Server errors . The session of awesome. I had never got such details explanation of the sql server error messages. He explained the best practices to write error handling code in sql server. Through it was session about error which every programmer hate it nobody was moved from his seat.

Then we had a tea break for 15 mins and after that Prabhjot Singh Bakshi has taken session on .NET Framework. The session was excellent and even advance concept was explain by simples examples. So overall it was great event and I enjoyed each and every session. Once again thanks for all the mentor of Ahmedabad .NET user group and Microsoft for organizing such events.

For more details about the event and photographs please visit following links.

As we all know we any web page on the internet is state less. We have to write our own mechanism to maintain state between httprequest and httpresponse. Asp.net has great features called viewstate to maintain state during page postback. It will store the element state in hidden variables with _VIEWSTATE. It will look like below.

If you are having large amount of controls then you will have larger viewstate on the page and it will increase your html kb as well as your performance. So to increase the performance of our application we need to store this viewstate in other place. ASP.NET 2.0 has nice new class called SessionPagePersister which will store the viewstate in the session. You just need to override the PageStatePersister property in your page and your viewstate will be on the page.

Here is the code.

Code Snippet

protectedoverridePageStatePersister PageStatePersister

{

get

{

returnnewSessionPageStatePersister(this);

}

\

If you are having so many pages then you have to write that cod manually Instead of doing this you can use inheritance features of C#.NET to write the code in just one way and then inherit your page from that class. Following is the code for that. You can create a class called MyPage that class into all the pages here is the code for class MyPage .

Code Snippet

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

namespace TestBlogApplication

{

publicclassMyPage:System.Web.UI.Page

{

protectedoverridePageStatePersister PageStatePersister

{

get

{

returnnewSessionPageStatePersister(this);

}

}

}

\

and Then inherit the class in your page like following.

Code Snippet

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace TestBlogApplication

{

publicpartialclassPage1 : MyPage

{

protectedvoid Page_Load(object sender, EventArgs e)

{

}

}

}

So that's it that way you can increase your application performance via maintaining the viewstate in session. Happy Codding..

In asp.net 3.5 there is one good features called extension method now you can extend your functionality without modifying existing classes. Extension method allow developers to add own functionality to any existing classes. You don't need to create subclass or don't need to recompile existing classes and still you can extend that class with extension methods. Let's create an example to extend existing string classes to convert a simple string to bold html string.

Microsoft SQL Server are one of the popular RDBMS(Relational Database Management System) all over world. Before some time Microsoft has launched the new version of SQL Server 2008. SQL Server 2008 provides the highest levels of security, reliability, and scalability for your business-critical applications. . Following are the some of new features of SQL Server 2008.

Policy based management.

Performance Data Collection.

Data compression.

Resource Generator.

Transparent Data Encryption.

External Key Management/Extensible Key Management.

Data Auditing.

Hot-Add CPU and Hot-Add Memory.

Streamlined Installation.

Server Group Management.

Upgrade Advisor.

Partition aligned indexed views.

Backup Compression.

Extended Events.

Grouping Set.

Merge Operator.

Greater Support for LINQ and Entity Framework.

Change Data Capture.

Table Valued Parameters.

Entity data model for entity framework.

Synchronization Server with ADO.NET.

CLR Improvements.

Conflict detection between peer to peer Replication

Service Broker Priorities and Diagnostics.

Spatial Data with Geography and Geometry data types.

Virtual Earth Integration.

Sparse Column.

Filtered Indexes.

Integrated full text search.

File stream data.

Large user defined types.

Large user defined aggregates.

Date/Time Data Types.

Improved XML Support.

ORDPATH.

and many more features are there. It's great rdbms to use in your future projects. Please visit following link to explore above features in great details.

When asp.net page loaded in to browser there are lots of white spaces between tags and in tags that will increase your html kb. For example if your page around 300 kb then it will take 3 second to load on 100 kbps internet connection and in dial up connection it will still take time. So if you want to load your site fast on dial up internet connection then you need to decrease html kb as much you can and removing white spaces from the html will be good idea for that.

Following is the code for the page on which you want to remove white spaces from the html. It will decrease your html kb by 30 percentage.

Here in the above code there are two regular expression one for white space in tag and another for whitespace between tag and by just overriding the render event of event you can replace the whitespace.

In earlier post i have mentioned that how to send the email using new System.Net.Smtpmail with asp.net 2.0 or higher version. Now lets learn how to send the mail with the secure service layer in the asp.net.

Security is the one of the most important requirement in today’s world as you are doing your business online you have keep secure from other wrong elements. Secure service layer add an

extra layer of security to your web application and sending mail with the SSL with tighten your security for mails. Your emails are more secure then ever and your valuable information will not going to wrong hands.

Here is the code from which we can send the email with the secure service layer. As i have mentioned in earlier post you need to to send email with authentication to use SSL. So you need to write following code. First you need to create message like following..

Client Id is used to store the id of a server control in client side. ASP.NET Engine creates a very long client id to unique identify each element on the same page. If you are having so many inner controls hierarchy then asp.net will generate very long client id like “ctl00_Ctl001_ctl02_BM_Login_btnSave” here i i have used control name like ctrl00,ctr001,bm etc. If you are having long control name then it will more longer then this. This will increase html kilobytes for the pages and if you are having so many server controls in the page then it will take time to load in browser and will decrease performance of application . To overcome this problem we have to override existing asp.net controls like textbox,label etc and then you have override client id from server side with server side id. Now with asp.net 4.0 you can control client id generated by .net framework. In asp.net 4.0 there is one another property called ClientIdMode which will control the behavior of client id generated by .net framework. Like following.

<asp:ButtonID="btnSave"runat="server"ClientIDMode="[Mode]"/>

There are four types of different mode for ClientIdMode property.

Legacy- This is the same as in old version. It will generate client like like “ctl00_Ctl001_ctl02_BM_Login_btnSave” in old fashion.

Inherit-This is the default mode for every control . It will refer parent control to have its clientidmode. If you want to change it then you have to specify your client id mode for that control.

Static –This mode will put client id as static if you set server id like “btnSave” then it will generate the same ld like “btnSave”. So if you want to use this mode then you have to make sure that you are having all unique name in one pages. Otherwise it will be a mess for ids.

Predictable- This mode will used when the framework needs to ensure uniqueness of each control in particular way. The framework traverse from the control to parent controls and put hierarchy of parent control id with control id like ‘”ctrl0_btnsave” and for another btnsave its like “ctrl0_ctrl1_btnsave” etc.

As an asp.net developer we all must have to use update panel for the ajaxifying our application in today’s ajax world. No one likes postaback now. But when we add the some content with the java script under update panel then some times it’s stop working because of the update panel server side behavior. So what we should do? … Here is the trick. Normally we code java script in the asp.net page like following…

As i have in my earlier post Search Engine optimization is necessary for every sites otherwise site will not have more users and viewers. For search engine meta tag are very important one Search engine crawler get information from this meta tags. There lots of meta tag but most important meta tag are keyword and description. From keyword meta tag search engine crawler get information about keyword for which they list this site page and from the description tag they will get descriptive information about particular page.

ASP.NET 4.0 introduces support for this. Now you can create your own meta tag keyword and description with the help of Page.MetaDescription and Page.MetaKeywords. It will render as same description and keyword meta tags in simple HTML Page. For example if you write following in your page_load event.