Friday, 9 December 2011

Check if a String Value Is Numeric in C#

This article will give you a way to determine if a given string has a valid number style (Integer, Float, Currency, etc.) according to specified format.
In all VBScript and Visual Basic applications, there is a built-in function called IsNumeric / IsCurrency, but in C# there is no built-in function with the same functionality.
In the code shown below, I actually use a built-in functionality of C# to determine if a given string value stands up to the requirements I want.

public bool isNumeric(string val, NumberStyles NumberStyle)
    Double result;
    return Double.TryParse(val, NumberStyle,
      System.Globalization.CultureInfo.CurrentCulture, out result);

The above function allows me to test if the given string stands up to one or more of the following styles:
  1. Hex Number
  2. Number
  3. Currency
  4. Float

isNumeric("7000", System.Globalization.NumberStyles.Integer)
If you want to test for an integer number separated with commas, then do the following:
isNumeric("7,51,000", System.Globalization.NumberStyles.Integer |

Tuesday, 6 December 2011

Web Application Projects Vs Web Site Projects in ASP.Net 4.0

Scenarios in which Web application projects are the preferred:
1.    You want to be able to edit code without stopping a debugging session.
2.    You want to run unit tests on code that is in the class files that are associated with ASP.NET pages.
3.    You want to refer to the classes that are associated with pages and user controls from standalone classes.
4.    You want to establish project dependencies between multiple Web projects.
5.    You want the compiler to create a single assembly for the entire site.
6.    You want control over the assembly name and version number that is generated for the site.
7.    You want to use MSBuild or Team Build to compile the project. For example, you might want to add pre-build and post-build steps.
8.    You want to avoid putting source code on a production server.
9.    You want to use the automated deployment tools that are available in Visual Studio 2010.

Scenarios in which Web site projects are the preferred:
1.    You want to include both C# and Visual Basic code in a single Web project. (By default, a Web application is compiled based on language settings in the project file. Exceptions can be made, but it is relatively difficult.)
2.    You want to open the production site in Visual Studio and update it in real time by using FTP.
3.    You do not want to have to explicitly compile the project in order to deploy it.
4.    If you do precompile the site, you want the compiler to create multiple assemblies for the site, which can include one assembly per page or user control, or one or more assemblies per folder.
5.    You want to be able to update individual files in production by just copying new versions to the production server, or by editing the files directly on the production server.
6.    If you precompile the site, you want to be able to update individual ASP.NET Web pages (.aspx files) without having to recompile the entire Web site.
7.    You like to keep your source code on the production server because it can serve as an additional backup copy.


Web application projects
Web site projects
Project file structure
A Visual Studio project file (.csproj or .vbproj) stores information about the project, such as the list of files that are included in the project, and any project-to-project references.
There is no project file (.csproj or .vbproj). All the files in a folder structure are automatically included in the site.
You explicitly compile the source code on the computer that is used for development or source control.
The source code is typically compiled dynamically (automatically) by ASP.NET on the server the first time a request is received after the site has been installed or updated.
By default, compilation of code files (excluding .aspx and .ascx files) produces a single assembly.
You can precompile the site (compile in advance on a development computer or on the server).

By default, compilation produces multiple assemblies.
Explicit namespaces are added to pages, controls, and classes by default.
Explicit namespaces are not added to pages, controls, and classes by default, but you can add them manually.
You copy the assembly to a server. The assembly is produced by compiling the application.
You copy the application source files to a computer that has IIS installed on it.
Visual Studio provides tools that integrate with the IIS Web deployment tool to automate many deployment tasks.
If you precompile the site on a development computer, you copy the assemblies produced by compilation to the IIS server.

Visual Studio provides tools for deployment, but they do not automate as many deployment tasks as the tools available for Web application projects.

Project File structure:
Web application projects use Visual Studio project files (.csproj or .vbproj) to keep track of information about the project. Among other tasks, this makes it possible to specify which files are included in or excluded from the project, and therefore which files are compiled during a build.
For Web site projects, all files in a folder structure are automatically considered to be included in the Web site. If you want to exclude something from compilation, you must remove the file from the Web site project folder or change its file-name extension to an extension that is not compiled and is not served by IIS.
An advantage of using project files in Web application projects is the following:
  • It is easy to temporarily remove files from the site but still make sure that you do not lose track of them, because they remain in the folder structure. For example, if a page is not ready to be deployed, you can temporarily exclude it from the build without deleting it from the folder structure. You can deploy the compiled assembly, and then include the file in the project again. This is especially important if you are working with a source control repository.
An advantage of using folder structure without project files in Web site projects is the following:
  • You do not have to manage the project's structure exclusively in Visual Studio. For example, you can copy files into the project or delete them from the project by using Windows Explorer.

For Web application projects, you typically build the project in Visual Studio or by using the ASP.NET batch compiler on a computer that is not the production IIS server. All code-behind class files and standalone class files in the project are compiled into a single assembly, which is then put in the Web application project's Bin folder. (The .aspx and .ascx files are compiled dynamically in a manner similar to what is done for Web site projects.)
For Web site projects, you do not have to manually compile the project. Web site projects are typically compiled dynamically by ASP.NET (on both the development computer and the production IIS server). You can choose between batch compilation mode, which typically produces one assembly per folder, and fixed compilation mode, which typically produces one assembly for each page or user control.
Advantages of the compilation model for Web application projects include the following:
  • You can use MSBuild to create a custom batch-compilation process.
  • It is easy to specify assembly attributes such as name and version.
  • Compiling in advance makes sure that users do not have to wait while the site compiles on the production server. (If the site is very large, dynamic compilation of a Web site project might take a noticeable amount of time. Dynamic compilation occurs when a request for a site resource is received after an update to the site, and the request that triggers compilation might be delayed while the required resources are compiled. If the delay is unacceptable, you can precompile the site. However, then some of the advantages of dynamic compilation are lost.)
  • You have complete control over where you put code files in the project folder structure, and how you how classes in the project refer to each other. (Dynamic compilation requires that the source code for any classes that are used throughout the site must be in the App_Code folder. You cannot refer to a page or user control class from a class in App_Code.)
Advantages of the compilation model for Web site projects include the following:
  • You can test specific pages regardless of the state of other pages. This is because running an individual page does not require that the whole site compile successfully, only the page and any components it depends on, such as code in the App_Code folder or the Global.asax file. (In a Web application project, if there are compilation errors anywhere in the site, you cannot create the assembly and therefore cannot test even the pieces of the site that compile.)
  • It is easy to update a Web site in production. You can update individual source code files on the production server without having to explicitly recompile the site. You can update individual files that are ready for deployment even if other files are not ready due to compile errors. You can also open the Web site on the production IIS server directly in Visual Studio and update the Web site in real time.
  • Precompiling to multiple assemblies can have a performance advantage in some scenarios. A typical example is a site that has many pages with lots of code written for them. Most of the pages are rarely requested and only some are used frequently. If you compile a site like this into multiple assemblies, the production server can load only the assemblies that are required for the current requests. If a page is not requested, its corresponding assembly is not loaded.

To deploy a Web application project, you copy the assembly that is created by compiling the project to an IIS server. In contrast, to deploy a Web site project, you typically copy the project source files to an IIS server.
Advantages of the deployment strategy for Web application projects include the following:
  • You can avoid deploying source code to the IIS server. In some scenarios, such as shared hosting environments, you might be concerned about unauthorized access to source code on the IIS server. (For a Web site project, You can avoid this risk by precompiling on a development computer and deploying the generated assemblies instead of the source code. However, in that case you lose some of the benefits of easy site updates.)
  • Deployment often involves other tasks in addition to copying assemblies or code to a server. For example, database scripts might have to run in production, and connection strings in the Web.config file might have to be changed for a production server. Visual Studio provides tools such as one-click publish that work with Web application projects to automate many of these tasks. These tools are not available for Web site projects.
Advantages of the deployment strategy for Web site projects include the following:
  • If you make a small change to a Web site, you do not have to redeploy the whole site. Instead, can copy just the changed file or files to the production IIS server. You can also edit files directly on the production server. (Because a Web application project's code files are compiled into a single assembly file, you must deploy the whole site even for small changes, unless the only change is to an .aspx or .ascx file.)

There is no difference in performance between a Web site project and a Web application project. The only significant exceptions are the ones that have already been noted, and as a practical matter they apply only to very large sites. The first request to the Web site might require the site to be compiled, which can result in a delay. And if the Web site is running on an IIS server that is short on memory, including the entire site in a single assembly might use more memory than would be required for multiple assemblies.