Skip to main content

Posts

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...

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...