TOD : Wrapping Code Block and Statements in Visual Studio

In this tips I have explained how you can automatically wrap up code blocks or code statement in Visual Studio. We can use Document formatting to format the whole contents, but that doesn’t wrap single line code block like Properties or not even single line multiple declaration statement.  Let’s consider an example, you are creating a Properties using code snippet and by default it will came up like

image

But, you want you code to be look like as below

image

How ? Read Complete Tips

How to make ViewState secure in ASP.NET ?

The ASP.NET ViewState is a client side state management.  ViewState is stored in hidden field with ID named __VIEWSTATE. Typically, stored ViewState information  looks as below:

image 

Now let us look at the value. It looks likes an encrypted string. This is nothing but Base64 Encoded string and it is not an encrypted string.  So it can be easily decoded.

The main reasons for making it Base64 encoding are as follows:

1.  Base64 makes a string suitable for HTTP transfer

2. It makes it a little harder to read .

But people often get confused that this is an encrypted string.

Add document header for files automatically in Visual Studio

In this blog post I am going to share  how you can add a document header imageof xml comments with code file automatically. This is going to be an very interesting and helpful for all of you who are using styleCop to maintain coding standard. Actually extensive using of StyleCop helped me to think to make it automate. There are many tools available which can helps us in this case to do this automatically. But I always prefer let’s Visual studio do the job for me.  Here I am going to share with  you two different approaches to deal with this issue. Now it’s up to you which one to use.

Tips : Set Breakpoint to multiple Functions at a same time in Visual Studio

Visual Studio having great features to set breakpoint in  multiple functions at same time.  For most of the time, to set a breakpoint  with in a function you just put a breakpoint in the first line of the function.  Now, assume you have 10 different overloaded methods or you have same function in different project, and you want to put breakpoint all of them or few of them, then it will take time to put breakpoint in each and every function.  In this post I am going to share two quick tips by which you can set breakpoint to function very easily. first one is by using “Break at Function” and second one by “Using Find Combo Box”.

How to use out of scope object with in Conditional Breakpoint ?

During debugging of your application, you may need to keep track of some data which are already out of scope. Say, You call a MethodB() from MethodA() and did some operation inside MethodB(), now when you returned  back to MethodA(), all the local variables inside MethodB() are out of scope. But  you want to use them in MethodA(). Now here is the trick, that you can create an ObjectID for an member variables or objects and can track the same when it’s out of scope as well, but until GC() does not collect it.  In this post, I am going to explore how we can use an Out of Scope object value to deal with Conditional Break points. To start with, first you have to understand how to use Object ID to track and out of scope object. Here is my complete article How to track an object which is Out of Scope while Debugging ? . Though I described a quick look into use of out of scope object inside conditional breakpoints on that article,  I received few query about more details on the same. So here I am going to give some more details.

How to track an object which is Out of Scope while Debugging ?

In Mastering in Visual Studio 2010 Debugging article I have discussed about the basic of Object ID creation while debugging. I received some request from some readers to explain the use of “Make Object ID” in details. In this blog post I am going explain how we can track an Object which is already out of scope using by creating a Object ID while debugging.

By using “Make Object ID” option we are informing Visual Studio Debugger to keep track of that object no matter it’s within scope or out of scope for the current context.  We can create “Object ID” either from Locals, Autos or from Watch Windows. Object ID is a integer number followed by a pound (#) sign. When we create Object ID for an particular object, Visual Studio Debugger ( CLR Debugging Services )  use an integer value to uniquely identify the object. This “Object ID” allows you to get the object details even if it is out of scope.

Customize the Debugging Windows : Change Debugging Window View as per your requirements

In this blog post I am going to explain how you can customize the complete view of the debugging window during debugging of application. By complete view means, where you can add own Properties, can customize the result, manipulate the data, hide properties which may not need during debugging etc. In one of my previous blog post, I have explained how we can customize the view of the debugging windows using DebuggerBrowseable and DebuggerDisplay attributes over Few Tips on Customizing Debugging Window View in Visual Studio . But, both these two attributes are limited to customize the view for a specific class and they can only show you the information for the particular class members . In this blog post you will see how we can create new view for some existing debugging view using “DebuggerTypeProxy” attributes. From the below snaps you can understand what kind of customization we can do inside a debugging window.

Overall

Use “Obsolete” attributes to indicate Obsolete Methods

In this post I have explained how you can use Obsolete attributes to mark some methods which are not no longer in used or may be remove in future release of the class.  During the development cycle we may need to change the name or declaration  of certain methods which may be using by some other developers. In this case if you changed the name or declaration of that method, application may crash in different point of times as it’s being used by other developer in the application. In this case you can use System.ObsoleteAttributes  class to generate compiler warning and indicate the method as Obsolete.

1

Few Tips on Customizing Debugging Window View in Visual Studio

In this post I am going to discuss about few tips on customizing debugging debugging windows. This may be very helpful during debugging of application. While debugging, you may want to simplify debug window or you may want to clean up all the unnecessary fields that are not important during debugging from debugging windows.  Here is few tips for customization of debug window. 

lightbulb Use DebuggerBrowsable attribute to customize the debugging windows

2

lightbulb  Use DebuggerDisplay attribute to customize the debugging display.

 1

 

To use above attributes you have to use System.Diagnostics namesapce

How to suppress compiler warning using #pragma warning directives in Visual Studio

Sometimes during development you may need to avoid the unnecessary compiler warnings to show in warning window. Though it’s not recommended to suppress the warning explicitly, but during some debugging and analysis of code you may want to hide them for time being. In this post I have explained how you can hide the unwanted warning explicitly by specifying the warning number.

To suppress the warning you need to use #pragma warning directives in your code. Below is the syntax for the same.

#pragma warning disable <warning-Numbers>

[Line of Code ]

Let’s consider you are having the below code block and you want to suppress the warning messages.

1

If you are going to build the above code, you will find the below warning messages.

2

Set source view as default view for Web Pages in Visual Studio

By default in Visual Studio for any web pages design view is the default view. Which may causes some time to load if your page is having many controls . So If you want to change the default view from Design View to Source View, just perform the following steps.

Go to “Tools” > “Options” and Navigate to “HTML Designer” Section.

sourceview

How to set automatically focus on ASP.NET controls when validation fails ?

If you are using ASP.NET Validation Control then you can use “SetFocusOnError” properties to automatically focus the control if validation fails. This will help the end user to identify the control easily.

To set focus automatically you  have to set SetFocusOnError=”True”. By default it’s false. SetFocusOnError  of a validation control is associated with “ControlToValidate” properties. If the validation fails, focus will automatically  move to the control which is specified in ControlToValidate.

SetFocus

How to setup multiple startup projects in Visual Studio ?

In this blog post I am going to describe a small tips of visual studio where you will get to know how you can  launch multiple project at same time. This is quite useful when you are working on a solution which having multiple project type and you want to run few of  them in same time.

Let’s assume you have a frontend application which developed using WPF and in the backend you are calling a WCF Service. Now to test the application you need both of them on running stage. By default Visual Studio project Setup type sets to “Single Startup Project” . If you set any of the project as "Start up” then that project will start when you run the application.  So after that you need to run the second project also. Below is the Project setup window where you can find the all the setting related with project start up. You can open that window by right clicking on Solution” > “Properties” > “Common Properties” > Startup Project”

8-11-2010 2-33-47 AM

Now, if you look into the above picture you will find we have three options for project setup.

How to sort ASP.NET DropDownList based on DataValueField or DataTextField using LINQ?

Sorting ASP.NET Dropdown list is very common requirement for any of the web application development.  To Implement this features sometimes developers used to iterate through each and every item and create a sorted list of element then reassign the same source to dropdownlist or sort the element at source itself. But this thing can be done easily using LINQ. In this post I am going describe how you can sort a ASP.NET DropDownList based on either of DataTextField or DataValueField using LINQ and list of KeyValuePair elements as DataSource.

To start with the application, let’s consider you have following List of employee data as datasource of the DropDownList.

 /// <summary>
 /// Create List Of Employee
 /// </summary>
 List<KeyValuePair<int, string>> employees = new List<KeyValuePair<int, string>>
        {        new KeyValuePair<int,string>(1,"Abhijit"),
                  new KeyValuePair<int,string>(2,"Rahul"),
                  new KeyValuePair<int,string>(3,"Kunal"),
                  new KeyValuePair<int,string>(4,"Atul"),
                 new KeyValuePair<int,string>(5,"Abhishek"),
        };
 

In the employee list collection, you have added KeyValuePair for each element, where Key is employee ID and Value is the employee name.  The most interesting part of using KeyValuePair is you can bind either of Key or Value with the DropDownList as per your requirement

Now let’s bind the DropDownList with the DataSource