Skip to main content

Posts

Showing posts from 2007

Open source Content editor

As most of the website these days have dynamic content, you need a good content editor. I have used this FCKEditor (http://www.fckeditor.net/). Here are few godd reasons to use it. 1. You can customised the toolbar to your need. 2. It is same functionality as MS Word. 3. Open source and hence free. 4. Cross browser compatible. 5. Cross platform compatible. 6. Available for multiple enviroments like .Net, php etc. Last but not the least....it is very simple to use. Raj

Importance of Namespace

Recently I have been developing a content managed website. The website worked fine on my machine (I can see developers grinning there!). The problem started when I published this website on a live server. It started giving out strange error like "no code found at line..." and it was pointing a dll in a temporary file in .Net framework folder. After some investigation, it turns out that my class names were clashing with others. I renamed the classes and class files and it sorted the problem. A proper solution to this would be to define you own namespace and make sure all the classes are within that namespace. This is also important if you are deploying website on shared server. Raj

Paths in asp.net

When working with resources in a Web site, you must often specify a path for the resource. For example, you might use a URL path to reference an image file in a page or the URL of a page elsewhere in the Web site. Similarly, code in your Web application might use a physical file path to a server-based file to read or write the file. Client Elements Elements that are not server controls on a page are client elements. There are two ways for specifying a path in client elements. A. Absolute path An absolute URL path is useful if you are referencing resources in another location such as an external Web site. For E.g. <img src="http://www.yourwebsite.com/MyApplication/Images/SampleImage.jpg" /> B. relative path Site Root relative path A site-root relative path, which is resolved against the site (not application) root. This example path assumes the existence of an Images folder under the Web site root: For e.g <img src="/Images/SampleImage.jpg&quo

Files in a ASP.Net project

Solution (sln) file. Solution user Options (.suo) file. Project configuration file (.vbproj) or .vcproj file .aspx, .asmx, .ascx .aspx.vb or .aspx.cs - code behind fiels globax.asax resouce file .resx other files .txt, .rpt style .css files congig file web.config. discovery file .disco or .vbdisco Project Assembly files (.dll)—All of the code-behind files (.aspx.vb and .aspx.cs) in a project are compiled into a single assembly file that is stored as ProjectName.dll. This project assembly file is placed in the /bin directory of the Web application. AssemblyInfo.vb or AssemblyInfo.vb or AssemblyInfo.cs—The AssemblyInfo file is used to write the general information, specifically assembly version and assembly attributes, about the assembly.

Application domain restart

There are several conditions that will cause an application domain to shut down and restart. When ASP.NET must restart an AppDomain, it simply removes the entry in the table that points to the AppDomain. When a new request arrives, the table does not have an entry; therefore, ASP.NET creates a new AppDomain that handles all of the future requests. The old AppDomain continues running until it completes all of its current requests. Finally, the old AppDomain shuts down. The following changes will cause an AppDomain to restart: Configuration change—If the machine.config or Web.config files are changed, ASP.NET detects the change and restarts the affected AppDomain. Global.asax change—If the global.asax file is changed, ASP.NET detects the change and restarts the affected AppDomain. Bin directory change—If an assembly in the bin directory is changed, ASP.NET restarts the AppDomain that corresponds to that bin directory. Compilation count change—When an ASP.NET Web page is changed, it is re

R.I.P Visual Basic

Are Visual Basic days numbered? Over last few weeks I have been hearing that Microsoft is going to stop supporting Visual Basic at some point in future. C# is the language of future. Here, according to some, are few evidences of it. 1. There are new versions of C# but no major development in VB.net 2. Microsoft uses C# for example in most of the articles/journals. Do a quick search on "VB.Net a dead language" and you will find some interesting article. I would like to hear your views on this. Personally I think C# is a much better language than VB.Net even though most of my experience has been in VB. The syntax of C# is much more clear and easy to understand. I must say I am converted and given a choice I will prefer coding in C#. DebugGuru

Sharing code across multiple pages/ web applications

There are times when you will want to share code across several pages in your site. There are 3 ways of doing it. 1. The code directory (APP_CODE). Just copy your .vb or .cs file in app_code and it will be dynamically compiled. 2. Local assembly (BIN directory). Compile your code and copy the dll file in BIN. 3. Global Assembly Cache. You can register a strongly typed assembly using regasm.exe. Alternatively you can register a assembly using web.config I will explain each of these in details in my next few blogs. DebugGuru

Shared code in multiple languages.

By default, the App_Code directory can only contain files of the same language. However, you may partition the App_Code directory into subdirectories (each containing files of the same language) in order to contain multiple languages under the App_Code directory. To do this, you need to register each subdirectory in the Web.config file for the application. <system.web> <compilation> <codesubdirectories> <add directoryname="CSharpCode"> <add directoryname="VBCode"> <add directoryname="CPPCode"> </codesubdirectories> </compilation> </system.web> </configuration>

Database deployment

In past it has been a pain for developers to deploy a database driven application. You had to go through a list of SQL scripts to create tables and other database objects. In ASP.Net 2.0, you can copy the . mdf file in \APP_DATA folder. Then XCOPY the entire application folder on the web server. Following one line of code in web. config will attach the database for you. connectionString =" Data Source =.\SQLExpress;AttachDbFile=|DataDirectory|\ASPNETDB.MDF;Database = dbASPNETDB . MDF ; User Instance=True; Trusted_Connection = Yes;" The above connection string assumes you have an instance of SQL Server called SQLExpress running on you local machine. ASPNETDB . MDF is available in APP_DATA folder. ASPNET is trusted user.

Performance improvement with StringBuilder class

Often we do large string concatenation in our code without giving a thought to performance. Consider following example of string concatenation. string s1 = "orange"; string s2 = "red"; s1 += s2; System.Console.WriteLine(s1); // outputs "orangered" s1 = s1.Substring(2, 5); System.Console.WriteLine(s1); // outputs "ange r" String objects are immutable, meaning that they cannot be changed once they have been created. Methods that act on strings actually return new string objects. In the previous example, when the contents of s1 and s2 are concatenated to form a single string, the two strings containing "orange" and "red" are both unmodified. The += operator creates a new string that contains the combined contents. The result is that s1 now refers to a different string altogether. A string containing just "orange" still exists, but is no longer referenced when s1 is concatenated. Therefore, for performance reasons, lar

Strings

The @ Symbol The @ symbol tells the string constructor to ignore escape characters and line breaks. The following two strings are therefore identical: string p1 = "\\\\My Documents\\My Files\\"; string p2 = @"\\My Documents\My Files\"; // Copy one character of the string (not possible with a System.String) sb[0] = sb[9]; System.Console.WriteLine(sb); // displays 9123456789 } }

Method returning multiple values

If you want methods to return more than one value, declare the parameters as OUT or REF. Methods with OUT or REF are identical at compile time but different at run time. Hence you cannot overload a method one with OUT where as other with REF. You can overload method by declaring one with OUT or REF and another without it. The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. Same applied for REF For example: class OutExample { static void Method(out int i, out int j) { i = 44; j= 22; } static void Main() { int value1; int value2; Method(out value1, out value2); // value1 is now 44 //value2 is now 22 } } Although variables passed as an OUT arguments need not be initialized prior to being passed, the calling method is required to assign a value before the met

Tracing in ASP.Net

1. To enable tracing for the application, use the element in the Web.config file or machine.config file. <trace enabled="true" pageOutput="true false" /> 2. To enable tracing for a single page, use the attributes of the @Page directive of that page. <%@ Page Trace="true" Language= ... %> 3. In your code, add custom trace messages where appropriate using the Write and Warn methods of the Trace object. In Visual Basic .NET: Trace.Write("Custom Trace", "Beginning User Code...") Trace.Warn("Custom Trace", "Array count is null!") In C#: Trace.Write("Custom Trace", "Beginning User Code..."); Trace.Warn("Custom Trace", "Array count is null!"); Optionally, use the IsEnabled property provided by the current page's TraceContext object (accessed by using the Trace property of the Page object). In Visual Basic .NET: If Trace.IsEnabled Then strMsg = "Tracing is en

Data size limit with Web Service Extension(WSE)

If you are using web service extension, watch out for size restriction based on the version of WSE 1. WSE 1.0 There is no size restriction on this. So if your web service and client with WSE 1.0, you won't have any problem. 2. WSE 2.0/3.0 In WSE 2.0/3.0, Microsoft introduced maxRequestLength which limits the amount of data transferred per request for a web service. By default this will be 4 MB (4096KB). You can set it to unlimited by setting it -1 as follows in web.config. <httpRuntime executionTimeout="90" maxRequestLength="-1" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100"/> or as follows in app.config for a windows based application. < microsoft.web.services2> < messaging > < maxRequestLength > -1 < /maxRequestLength > < /messaging &

Application deployed on a web farm - View state validation.

By default, ASP.Net 2.0 encrypts the data in the ViewState with a private key generated on the server. This key is unique to the server. So in a scenario where there is more than one server e.g. in a web farm, each server will generate its own key. This is where the trouble starts. Consider there are 3 servers A, B and C. Say users first request is served by server A. Now when user posts the data back, say the request is sent to server B. Server B will now try to validate the data in ViewState with its own key. As the key is different to the one with which the data was encrypted, it will fail validation and will raise "Unable to validate data" error. To avoid this, configures MachineKey element in Web.Config for a cohosted website or in Machine.Config for dedicated server. Please see http://msdn2.microsoft.com/en-us/library/ms998288.aspx on how to configure machinekey. DebugGuru

Testing performance of ASP.Net Application

I have been looking at various ways of testing the performance of ASP.Net applications. Here are few tools that might be useful. 1. Application Test Center by Microsoft If you have Microsoft Visual Studio 2005 enterprise version, then you will have this software included. Please see http://msdn.microsoft.com/msdnmag/issues/05/06/ExtremeASPNET/ on how to use this great tool. 2. BadBoy This is another good software. Please visit http://www.badboy.com.au/ to download the software. This software can be used for free if you are a non profit organisation. 3. LoadRunner If you are a commerical organisation and have a good size budget, this is the software for you. Please visit http://www.mercury.com/us/products/performance-center/loadrunner/ for more information. This software gives you lot more control and has good reporting tools as well. You need special training to use this software or you can have a consultant doing the job for you. Happy performance testing. Thanks DebugGuru