30 October 2011

How to not show Visual Studio line numbers for blank lines?

Is there a way to display only line number of coded lines, i.e. don’t number blank lines? Numbered blank lines makes showing code in powerpoint decks hard to follow. 
 
Here’s the screenshot 
To do this,
  1. Enable Word Wrap (Tools Options – Text Editor – All Languages – General)
  2. At the end of the first real line (like #28 above), start inserting white space until it goes off the screen. Note the Word Wrap glyph.
  3. Turn off the Word Wrap glyph (same place as in step 1) and Turn off “View White Space” (Edit – Advance – View White Space)

HTML5 Intellisense in Visual Studio 2010/2008

HTML5 provide a great intellisense for Visual Studio 2010/2008.  You can download that tool from following link.

Once you download HTML5 then install it and then you need to change your validation to HTML5 in your Visual Studio 2010/2008 configuration. For that you have to follow bellow steps.
Go to Tools -> Options ->(Screen1) and then Text Editor -> HTML -> Validation and there you need to select the HTML5 like following.  as shown below screen 2 and then click on button.

Screen1

Screen 2
Now Visual studio 2010 or 2008 will have intellisense for HTM5. 


21 October 2011

Visual Studio 2008 styles

Visual Studio allows you to completely customize the editor background and text colors to whatever you want – allowing you to tweak them to create the experience that is “just right” for your eyes and personality.  You can then optionally export/import your color scheme preferences to an XML file via the Tools->Import and Export Settings menu command.
  
A New website that makes it easy to download and share VS color schemes

Luke Sampson launched the http://studiostyles.info/ site a week ago (built using ASP.NET MVC 2, ASP.NET 4 and VS 2010). Studiostyles.info enables you to easily browse and download Visual Studio color schemes that others have already created.  The color schemes work for both VS 2008 and VS 2010 (all versions – including the free VS express editions):

You can click any of the schemes to see screen-shots of it in use for common coding scenarios.  You can then download the color settings for either VS 2010 or VS 2008: This site is cool and you can find lots of visual studio 2010 and 2008 styles of your choice. 



I have downloaded the first one(Son of Obsidian). It will be visual studio settings file. 


Once you downloaded you can import like following. GoTo Tools->Import and export settings in Visual Studio 2008. Once you click this It will import and export dialog like following.

I have selected Import selected environment setting and clicked next on next screen you will have option to save your visual studio settings like following.

Clicking on next you will have options for different visual studio 2008 Style like following.

 Click on browse and select your downloaded style like following.

Click on open and then you will represented to recent file options

Click next You will come to final settings wizard like following.


 Click on finish now our style is applied.Close the dialog and open your project and you can see new color scheme for your visual studio 2008 like following.

How to Open Visual Studio Quickly?

Do the following simple steps.
Open Your Visual Studio by typing 'Start' --> 'Run' --> 'DEVENV' then,

1. Click Tools
2. Select Options.
3. Expand Environment.
4. Click Startup.
5. Choose Show Empty Environment in the dropdown for At startup.
6. Disable the Download Content Every checkbox
7. Click OK.  as shown in the below screen shot. 


27 April 2011

What is Web.Config And Machine.Config File?

What is Web.Config File?
It is an optional XML File which stores configuration details for a specific asp.net web application. 
Note:  When you modify the settings in the Web.Config file, you do not need to restart the Web service for the modifications to take effect..  By default, the Web.Config file applies to all the pages in the current directory and its subdirectories.
Extra:  You can use the <location> tag to lock configuration settings in the Web.Config file so that they cannot be overridden by a Web.Config file located below it. You can use the allowOverride attribute to lock configuration settings. This attribute is especially valuable if you are hosting untrusted applications on your server.

What is Machine.config File?
The Machine.Config file, which specifies the settings that are global to a particular machine. This file is located at the following path:
 \WINNT\Microsoft.NET\Framework\[Framework Version]\CONFIG\machine.config
As web.config file is used to configure one asp .net web application, same way Machine.config file is used to configure the application according to a particular machine. That is, configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.
You can override settings in the Machine.Config file for all the applications in a particular Web site by placing a Web.Config file in the root directory of the Web site as follows:
\InetPub\wwwroot\Web.Config
  
What can be stored in Web.config file?
There are number of important settings that can be stored in the configuration file. Here are some of the most frequently used configurations, stored conveniently inside Web.config file..
1.      Database connections.
2.      Session States
3.      Error Handling (CustomError Page Settings.)
4.      Security (Authentication modes)

What is the best place to store Database connection string?
In Web.Config, you would add a key to the AppSettings Section:

<appSettings>
 <add key="MyDBConnection" value="data source=<ServerName>;Initial catalog =<DBName>;user id=<Username>;password=<Password>;" />
 </appSettings>

Example:
<add key="ConnectionString" value= "data source=localhost;Initial catalog=northwind;user id=sa;password=mypass" />
Then, in your ASP.Net application - just refer to it like this:
using System.Configuration;
string connectionString = (string )ConfigurationSettings.AppSettings["ConnectionString"];

Difference between Web.Config and Machine.Config File

Two types of configuration files supported by ASP.Net.
Configuration files are used to control and manage the behavior of a web application.

i) Machine.config
ii)Web.config

Difference between Machine.Config and Web.Config
Machine.Config:
i)  This is automatically installed when you install Visual Studio. Net.
ii) This is also called machine level configuration file.
iii)Only one machine.config file exists on a server.
iv) This file is at the highest level in the configuration hierarchy.

Web.Config:
i)  This is automatically created when you create an ASP.Net web application project.
ii) This is also called application level configuration file.
iii)This file inherits setting from the machine.config

13 April 2011

difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\") and Server.MapPath("/")?

Server.MapPath specifies the relative or virtual path to map to a physical directory.
  • Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
An example:
Let's say you pointed a web site application (http://www.example.com/) to
C:\Inetpub\wwwroot
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
D:\WebApps\shop
For example, if you call Server.MapPath in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
then:
  • Server.MapPath(".") returns D:\WebApps\shop\products
  • Server.MapPath("..") returns D:\WebApps\shop
  • Server.MapPath("~") returns D:\WebApps\shop
  • Server.MapPath("/") returns C:\Inetpub\wwwroot
  • Server.MapPath("/shop") returns D:\WebApps\shop
If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path.
If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.
Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.

18 March 2011

Temp table VS Table variable

Most of the SQL Developers/DBA would have come across a situation where they need to store the temporary result sets. This is where Temp tables and Table variables come into effect and helps in storing the data sets in a temporary location.


Temp table:

Consider the below sample temp table which holds the information about companies.
CREATE TABLE #Tmp
(
CompanyId Int,
Name varchar (50),
Location varchar (50)
)

  1. The temp table name always starts with # or ## and are created in the tempdb database. The # indicates that the temp table is a local temporary table i.e. table is accessible only by the particular connection of SQL Server which created it. The ## indicates that the temp table is a global temporary table i.e. the table is accessible from any connection. They are dropped automatically when the last session that uses them has completed.
  2. Since the local temporary table is accessible only by the connection which created it, this helps in minimizing the locks.
  3. We can create indexes, statistics in temp tables and hence performance can be improved.
  4. We cannot have foreign key constraints on temp tables.
  5. Causes recompilation within stored procedures.
  6. Only undo information is logged in tempdb and not the redo information.
  7. We can Rollback the transactions in temp table similar to a normal table but not in table variable.
  8. Temp tables can be used in nested stored procedures.
  9. The temp table names cannot exceed 116 characters whereas the permanent table can have 128 characters

The following example illustrates the transaction behavior in Temp tables:

--using temp tables where ROLLBACK happens
CREATE TABLE #Tmp
(
CompanyId Int,
Name varchar(20),
Location varchar(20)
)
GO
 
INSERT INTO #Tmp
VALUES (1,'Deepak','Chennai')
GO
 
BEGIN TRAN
UPDATE #Tmp
SET Location='CH'
WHERE CompanyId=1
ROLLBACK TRAN
 
SELECT * FROM #Tmp


Table variables:

The following the syntax for table variables:

DECLARE @Tmp TABLE
(
CompanyId Int,
Name varchar(20),
Location varchar(20)
)

  1. Table variables are local to a stored procedure and hence cannot be used in nested stored procedures
  2. We cannot create Nonclustered indexes in Table variables only Clustered index can be created by specifying them as constraints (Primary or Unique)                                                                                                                                                        DECLARE @Tmp TABLE (C1 int, C2 int, PRIMARY KEY (C1, C2))
  3. Table variables store the contents in memory but not always. Under extreme memory pressure, the pages belonging to table variables will be moved to tempdb
  4. We cannot Alter a table variable once its declared
  5. We cannot create statistics in table variables
  6. They cannot make use of multiple processors and hence Parallelism is not possible
  7. Transactions cannot be rollbacked in Table variable

The following example illustrates the transaction behavior in table variables:

--using table variables where ROLLBACK NEVER happens
DECLARE @Tmp TABLE
(
CompanyId Int,
Name varchar(20),
Location varchar(20)
)
INSERT INTO @Tmp
VALUES (1,'Deepak','Chennai')
 
BEGIN TRAN
UPDATE @Tmp
SET Location='CH'
WHERE CompanyId=1
ROLLBACK TRAN
 
SELECT * FROM @Tmp

I tried the following to check the performance perspective of table variables and temp tables. I could see that Temp tables are quite faster than table variables if we load numerous records. However with <10000 records being loaded, the table variables were much faster than temp tables.

I have a table named testmember with 1.5 million records.

--took 52 seconds to complete
SET STATISTICS TIME ON
DECLARE @Tmp TABLE
(
memberid          bigint,
name    nvarchar(100),
firstname           nvarchar(100),
emailaddress     nvarchar(100)
)
INSERT INTO @Tmp
SELECT memberid, name, firstname, emailaddress FROM testmember
WHERE memberid between 1 and 1000000
 
SELECT T.memberid, T.name, T.firstname, T.emailaddress
FROM @Tmp T INNER JOIN testmember M
ON T.memberid=M.memberid
where M.Memberid=1000
SET STATISTICS TIME OFF
 
--DBCC DROPCLEANBUFFERS
 
--took 45 seconds to complete
SET STATISTICS TIME ON
CREATE TABLE #Tmp
(
memberid          bigint,
name    nvarchar(100),
firstname           nvarchar(100),
emailaddress     nvarchar(100)
)
INSERT INTO #Tmp
SELECT memberid, name, firstname, emailaddress FROM testmember
WHERE memberid between 1 and 1000000
 
SELECT T.memberid, T.name, T.firstname, T.emailaddress
FROM #Tmp T INNER JOIN testmember M
ON T.memberid=M.memberid
where M.Memberid=1000
SET STATISTICS TIME OFF

Since we can create indexes, statistics etc there is still a scope for further improvement in performance in temp tables. In general there is no hard and fast rule, if there are <10K records we can opt for table variable else use temp tables but always test the query and then take a decision.

Limitations of Temp tables and Table variables:

  1. There will be high load on the disk where tempdb resides if temp tables are used frequently and to a large extent and we have to keep an eye on the tempdb growth to ensure that it doesn’t become full and consume disk space
  2. Table variables will perform poorly with large record set since index cannot be created other than primary key (Clustered Index)

17 March 2011

There is no source code available for the current location

I have a problem when i am started running windows application.  I copied that form one system to another, i got one as shown bellow. 
Debugging Error.
"There is no source code available for the current location. vs2005..."
Solution:-
Erase .pdb files that i have in my debug folder and it is working fine.
Reference Link:-
http://social.msdn.microsoft.com/Forums/en/csharpide/thread/5ee88200-cee8-44f4-a46a-774044c1ef38

Globalization using ASP.NET


Sometimes our application may need to cater content in different languages for different country users. Let us see in this article how to cater different contents based on the culture using Globalization in ASP.NET.

Globalization is the process of designing and developing a software product that function for multiple cultures. A web forms page have two culture values ,Culture and UICulture. Culture is used to determine culture dependent function such as Date, Currency. So it is used for date formatting ,number formatting. UICulture values is used to determine which language the resources should load that is which UIstring the resource should use. The two culture settings do not need to have same values. It may be different depending on the application.

Setting Culture and UICulture
 
1. Through Web.Config
<configuration>
<
system.web>
<
globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US" uiCulture="fr-FR"/>
</
system.web>
</
configuration>

2. In Code-inline (aspx) Page
<%@ Page UICulture="fr" Culture="en-US" ....%>

Now, suppose I want to change the current culture programmatically, I can use following code to set the new culture.

3. In Code-Behind (aspx.cs) Page

using System.Globalization;
using System.Threading;

protected void Page_Init(object sender, EventArgs e)

    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
    }


Reference Links:-


http://msdn.microsoft.com/en-us/library/bb386581.aspx

07 March 2011

Error in File UNKNOWN.RPT: The request could not be submitted for background processing

I have a web application that uses Crystal Reports. It is working fine in one of our test server (Windows 2003). When we migrate this asp.net web apps to the production server, my Report Generate button that uses Crystal Reports did not work and got this following error.  The request could not be submitted for background processing".


Solution:-
I took the below steps to correct it:
  1. Right Click on C Drive
  2. Click on Security Tab
  3. Add "Network Services" to the list of users. Caution: Remember to remove it later.
  4. Browse to you application and crystal report should work now.
  5. Go back to your server and Remove the "Network Service" user from the security list. 
Hope this fixes the issue for all.