100 Useful .NET Tips & Tricks

Daily .NET Tips is aiming to sharing useful coding tips and tricks for .NET Developers. This site completely design for sharing Tips and Tricks, useful Code Snippet which anyone use in daily development work and targeted anything related with .NET.  I started with this new site few months back and with in short time period we have total 100 Tips which mainly focused on the practical oriented problem. You can find tips related with C#, ASP.NET, Silverlight, WPF, WCF, JQuery, WP7 etc.

To know more ……

Light bulbVisit  http://dailydotnettips.com

Light bulbSubscribe Tips to your mail box : http://bit.ly/hFAhop

Light bulbFor Quick Update Follow : @dailydotnettips 

Thanks to all the contributors Abhishek Sur, Prayan Rana, Jebarson Jebamony, Atul Verma,Shravan Kumar  who spends their time and shared tips.

Looking forward more contribution from all of you.

Freezing If you want to contribute your tips at  http://dailydotnettips.com , please contact with me via Contact Page.

Thanks !

AJ

Generic Way to Bind Enum With Different ASP.NET List Controls

In this post I am going to explain how we can bind a Enum with ASP.NET List Controls. These comprise of four different types of control, CheckBoxList, DropDownList, ListBox and RadioButtonList. This is one of the common requirements during development to bind a Enum with List Control and challenge is when we need to bind the both text and value.
Let’s consider we have the blow Enum. Now I am going to bind it with all the above mentioned type List Controls.
image

Well, One of the easiest way that we can implement is by Enum.GetNames(Type enumType)which retrieves the name list of enumberation  and Enum.GetValues(Type enumType), which returns the list of values for each names .

10 ASP.NET Tips and Tricks

Here I am sharing 10 ASP.NET Tips and Tricks most of them related with Grid View Control

How to change GridView column alignments for dynamic data source ?

How to pass multiple values using GridView HyperLinkField ?

Displaying Custom Messages / Images with NULL Value in ASP.NET GridView

Display custom messages or images when there is no records in GridView Data Source

How to Display “Yes” or “No” Instead of Checkbox while binding Boolean value with GridView ?

How to expand specific Tree View Node programmatically in ASP.NET ?

Run ASP.NET Web Application from Command Prompt

 How EnableViewStateMAC Makes ViewState Secure ?

 Disabling button when performing some operation in ASP.NET

 How to remove a persistent cookies before Expiration time ?

To know more Tips and Tricks Follow @dailydotnettips and visit http://dailydotnettips.com

On a side note,  I would like to congratulate couple of my friends, first a key community contributor on WCF, Dhananjay Kumar  for renewing  Microsoft MVP – Connected System again for year 2011. He blogs at  http://debugmode.net/ and his twitter id @debugmode_ .  and second one  Krishna Chaitanya T for Microsoft MVP – Internet Explorer . He is a Web Geek, do lots of research on web technology. He blogs at http://www.novogeek.com/ and his Twitter ID @novogeek . Good job guys !!

ASP.NET Custom Repeater Control with EmptyTemplate, ShowHeaderWhenEmpty,ShowFooterWhenEmpty,ShowCount Properties

If you have worked with ASP.NET GridView Control you must be aware of GridView.EmptyDataTemplate Property  which Gets or sets the user-defined content for the empty data row when a GriimagedView control  data source has no records. Similarly ShowHeaderWhenEmpty property allows you to show or hide the Header row when there is no records. These are the really good and very frequent required properties. Now, if you are working with the Repeater Control, it doesn’t have any direct features which enables you to set user define content when data source is empty. In this blog post I will discuss how we can create a custom ASP.NET Repeater control with support of EmptyTemplate along with few more important features.

Download PPT – “Caching Enhancement in ASP.NET 4.0” – Virtual Tech Days – 9th Feb 2011

Today I delivered an 1 hour session on “Caching Enhancement in ASP.NET 4.0”  at Microimagesoft Virtual Tech days 2011. It was the Day 1  of Virtual Tech Days and I presented the 4th session  on Developer Track 1, from 3.00 PM To 4.00 PM IST. Thanks to all of you for attending my session. In this session I have discussed about ASP.NET 4.0 Caching Enhancement . I started with fundamentals of ASP.NET Caching to make sure all the attends are in same page. In Caching Quick Tour section I have talked about why Caching ? What are the different types of caching ? How Caching works ? etc. Then I have discussed about New Object Caching API in .NET 4.0. In this section I have talked about new Memory Cache API, there Implementation using System.Runtime.Caching . I have also shown how we can use same set of API in an Winforms / WPF / Console Application. Followed by, I have explained how we can extend the Object Caching by implementing MemoryCache Class. 

Details explanation on Compression Enabled Session for SQL Server and State Sever Session Mode in ASP.NET 4.0

While we are talking about state management we consider Session is one of  most useful server side  state management mechanism for a web application.  In ASP.NET mainly we are having two type of state management  1. In Process  and 2. Out Process . In process is the by default session imagestorage mode for ASP.NET Web application and this is taken care by worker process in IIS. When it comes under Out Process we can use either of state server or SQL Server to persist session data. In case of In Process, session data stored in In memory of worker process. But when we are talking about OutProc session mode, we need to ensure that session data should be Serializedfirst . So, when we are moving session data from Web Server to Out Process Server ( State Server or SQL Server ) it can be a performance overhead based on the size of data that we are storing in Session.

ASP.NET 4.0 comes with a new option for compressing the Session data with Out Process Session mode. To enabling this functionality we need to add “compressionEnabled=”true” attribute with the SessionMode in web.config .

Programmatically Changing Session State Behavior in ASP.NET 4.0

Session is one of most important state management in ASP.NET. We can enable or disabled session state either in web.config or using @Page directive’s   EnableSessionState attributes. But there was no provision to change the session state at runtime till date in ASP.NET. But using  ASP.NET 4.0, we can change the session  state programmatically . The .NET 4.0 framework adds a new method SetSessionStateBehavior  to the HttpContext class for ASP.NET. This method required SessionStatebehavior  value to set the current session mode. To call SetSessionStateBehavior   simply create a new HttpModule by Implementing IHttModule and hook the BeginRequest event. Most important you can only use the SetSessionStateBehavior  until the AcquireRequestState event is fired, because AcquireRequestState  Occurs when ASP.NET acquires the current state  that is associated with the current request

While calling SetSessionStatebehavior, You can pass the following values as SessionStatebehaimagevior  :

  • Default: This is default setting which means  everything works as before
  • Disabled: Turned of Session Sate for Current Request.
  • ReadOnly: Read only access to Session State;
  • Required: Enabled session state for both Read and Write Access;

When we can use HttpContext.Current.Items to stores data in ASP.NET?

To answer this question In a single statement, you can use HttpContext.Current.Items for very short term storage. By Short term storage means, this data is valid for a single HTTP Request.  There are many confusion around regarding storing data in HttpContext.Current.Items and storing data in Session variable. In this blog post I am going to describe what are the different scenarios where we can use HttpContext.Current.Items and what is the exact difference with session variable.

Items collections of HttpContext is and IDictionary key-value collections and that are shared across a single HTTPRequest. Yes, HttpContext.Current.Items  valid for  a single HTTPRequest.  Once after processing, server  information  is sent back to the browser, the variables that were set in the Items[] collection will lost. Where as for Session Variable, information valid for multiple request as this is user specific. The session variable only  expires either on Session Time Out or explicitly clear the values.

Microsoft Community Tech Days – Hyderabad – 18th December 2010

Microsoft Community Tech Days in Hyderabad Scheduled on 18th December 2010. This time I will be speaking on one of my favorite topic “ASP.NET 4.0 State Management Improvement – Deep Dive”.

What is Community Tech-Days?

The Community Tech-Days website says “Dive deep into the world of Microsoft technologies at the Community TechDays and get trained on the latest from Microsoft. Build real connections with Microsoft experts and community members, and gain the inspiration and skills needed to maximize your impact on your organization while enhancing your career”

Community Tech-Days (CTD)  is a Microsoft technology event organized by Microsoft and Local Microsoft User Groups  to help developers and IT Prof. to catch up with the latest technologies and development methodologies.

How to Use ASP.NET “Property Pages” to create or host a Silverlight application in ASP.NET Site ?

While creating a Silverlight application in Visual Studio 2010, after providing the application name, IDE asked for whether you want to host the Silverlight application in a new ASP.NET web site or not. If you select the “Ok” button , Visual Studio will create a ASP.NET Site to host the corresponding Silverlight Application. So whenever you will run the Silverlight application, ASP.NET site will automatically host the Silverlight file  But suppose you have an existing Silverlight Application and you want to host it in a ASP.NET site in a similar way that New Silverlight project does  or you want to add a new Silverlight application which will automatically host inside your current asp.net web site? Do you know we can easily achieve this using “Property Pages” of an ASP.NET Application. Let’s explore how we can do that.

.NET Community Event – ASP.NET 4.0 In-depth ( II )

In continuation of our previous session on ASP.NET 4.0 – In Depth , We are going to take remaining part of the  session on 10th October 2010.  asp-net-4-0-session-agenda

We will be covering below topics

  • State Management Enhancements
  • Client side Enhancements
  • Deployment
  • Overview of Dynamic Data 

    Rest of the topic we have already covered in our previous session. 

  • You can download the videos and presentation of previous session from  here.

    Details of the Session :

    Speakers: Abhijit Jana & Abhishek Sur

    Scheduled Date: 10th – October- 2010  (Sunday )

    Scheduled Time: 2.00 PM to 4.30 PM – IST

    Live meeting URL: https://www.livemeeting.com/cc/mvp/

    Meeting ID:  Will be send from DotNetFunda.Com upon registration

    Is Paid?: No, It’s FREE Session

    Is Registration required ? Yes . Please register here http://bit.ly/d6aVYM .

    Note: Members who had already nominated for the first part of this session happened on 19-Sep-2010 need not re-nominate.

    Thanks !

    AJ

    Different Version of Integrated Web Server in Visual Studio 2010

    While running your ASP.net application you might have found it to be running on Integrated Web development environment. Visual Studio 2010 comes with two version of Integrated Web Development environment which could run side by side. In case you run your ASP.NET application in VS 2010 you can have an option to choose between the two version of Web Development environment, one running CLR 2.0, which was there before VS2010 and a new version of Web Dev with CLR 4.0. Multi – targeting allows you to configure your visual studio to run more than one Web Dev server at a time one using CLR 2.0 and another using CLR 4.0

    In one of my ASP.NET Online session I had talked about the multi targeting features of Visual Studio 2010 for web development. During the demo I have shown, how we can target two different version of .NET CLR version same time. Some of the attendees having some doubts on that, So in this blog post, I am going to explain how we can target two different version of CLR at same time.

    To demonstrate it, Start with a new Visual Studio Instance, and Create a solution file which contains two different Framework version of web applications.  Let’s say there are two web application which are targeting to ASP.NET 3.5 and ASP.NET 4.0 version.

    1 2

    What is the difference between Web Farm and Web Garden ?

    I have been asked this question many times by different readers of my blog. They wanted to know about the fundamentals of Web Farms and Web Garden. In this blog post, I am going to explain the what is the exact difference between web farm and web garden, what are the advantages and disadvantages of using them. I have also described how to create web garden in the different version of IIS.

    Overview :

    Visual Studio is having its integrated ASP.NET engine which is used to run the ASP.NET Web application from Visual Studio. ASP.NET Development Server is responsible for executing all the request and response from the client. Now after the end of development, when you want to host the site on some server to allow other peoples to access, the concept of web servers comes in between.  A web server is responsible for responding to all the requests that are coming from clients. Below diagram showing the typical deployment structure of an ASP.NET Web application with a single IIS.

    2 Clients request for resources and IIS Process the request and send back to clients. If you want to know more details on How IIS Process the request, please read one of my article over “How IIS Process ASP.NET Request ?”.

    Web Farm :

    This is the case, where you have only one web server and multiple clients requesting for the resources from the same server. But when there are huge numbers of incoming traffic for your web sites, one standalone server is not sufficient to process the request.  You may need to use multiple servers to host the application and divide the traffic among them.  This is called “Web Farm.” So when you are hosting your single web site on multiple web server over load balancer called “Web Farm.” Below diagram showing the over all representation of Web Farms.

     Web Farms

    How to remove Hyperlink from ASP.NET TreeView Control Nodes ?

    ASP.NET Tree view control rendered as HTML Table – TR –TD  elements. Each of the node are been represented as hyperlinks. Some times you may not want that fields as hyperlink and you want to be those nodes should be represent as static text. Many of the developer did the same using  font style or css style change of the node, but it can be done very easily.  In this small blog  post I am going to describe how you can represent a tree view node as simple text instead of hyperlink.

    This can be done very easily using Tree Nodes, SelectionAction properties. SelectionAction having 4 different values, they are.

    3

    Select is the default option which marked node as hyperlinked and on selection of the node, it raised SelectedNodeChanged event.  For Expand Option, TreeNodeExpanded event will be raised while expanding the node. “SelectExpand” raised both the SelectedNodeChanged and  TreeNodeExpanded  events. Now if you don’t want to make the node as simple text, just change the SelectionAction properties to “None”.

    1