Tuesday, November 4, 2008

COM- Early binding and Late binding in Automation

Classic COM doesn't exist in a managed environment, because it executes as instructions native to Windows.COM objects can be brought into managed environment by using a wrapper (the runtime callable wrapper). Two methods can be used to access a COM object
-early bound access (types are known at compile time) -[tlbimp.exe or vs.net Add reference]
-late bound access (type discovery and method invocation are performed at runtime).
Which form of binding is best?
The answer to this question depends as much on the design of your project as anything else. Microsoft recommends early binding in almost all cases. However, there may be reasons for choosing late binding.
Early binding is the preferred method. It is the best performer because your application binds directly to the address of the function being called and there is no extra overhead in doing a run-time lookup. In terms of overall execution speed, it is at least twice as fast as late binding.
Early binding also provides type safety. When you have a reference set to the component's type library, Visual Basic provides IntelliSense support to help you code each function correctly. Visual Basic also warns you if the data type of a parameter or return value is incorrect, saving a lot of time when writing and debugging code.
Late binding is still useful in situations where the exact interface of an object is not known at design-time. If your application seeks to talk with multiple unknown servers or needs to invoke functions by name (using the Visual Basic 6.0 CallByName function for example) then you need to use late binding. Late binding is also useful to work around compatibility problems between multiple versions of a component that has improperly modified or adapted its interface between versions.
The advantages given to early binding make it the best choice whenever possible.
for more details visit here

Friday, October 24, 2008

Tips for designing a good web site

All the web designer works on their way to develop the fast loading site. These are the common observation while we do web page design
1. Minimize the use of images.
2. Optimize images for the web
3. Use Tables creatively
4. Cut down the use of animated gifs
5. Design simple icons
6. Use background images instead of big images whenever possible
7. Try out CSS Styles
8. Use Flash sparingly
9. Design most of site contents in HTML.
Keep checking your load time - Check its load time on
NetMechanic. This site gives you a free analysis of your web site, which is extremely useful.


Asp.net Server Side coding tips

ASP.NET has been developed with lots of features, which makes developer life easy. Start from ViewState, PostBack, Server Controls , User Controls and etc. All of them can make your web development experience fun and easy, but improper use of them can cause your site performance slowdown.
1.Avoid unnecessary round trips to the server. Use IsPostBack property effectively.
2.Enabled ViewState only if needed.
3.Leave buffering on unless we have a specific reason to turn it off.
4.Use the Server.Transfer method from the Server object or cross-page posting to redirect between ASP.NET pages in the same application
5.Disable session state when you are not using it.( <
%@ Page EnableSessionState=false%> ) When required choose the correct session-state provider depends on the design.
6.Use SQL Server and stored procedures for data access. Use the SQLClient class and SqlDataReader class for a fast forward-only data cursor.
7.Cache data and page output whenever possible.
8.Avoid using view state encryption unless necessary.
9.Run Web applications out-of-process on Internet Information Services 5.0.
10.Recycle processes periodically.
11.Adjust the number of threads per worker process for your application if necessary.
12.For applications that rely extensively on external resources, consider enabling Web gardening on multiprocessor computers.
13.Disable debug mode, deploy only Release version. Use Pre-compiling deployment.
14.Avoid single-threaded apartment (STA) COM components.( include the ASPCompat=true attribute in the @ Page directive in the .aspx)
15.Handle exceptions wisely.

Monday, October 20, 2008

To debug JavaScript in VS.NET

  1. Open Microsoft Internet Explorer.
  2. On the Tools menu, click Internet Options.
  3. On the Advanced tab, locate the Browsing section, and uncheck the Disable script debugging check box, and then click OK.
  4. Close Internet Explorer.
    In your JavasSript function add the keyword debugger . This causes VS.NET to switch to debug mode when it runs that line.
    Run your ASP.Net application in debug mode.
    That's all there is to it.

Working with large XML files in asp.net

When we work with larger XML files(2GB), if we use XMLDocument object which takes the whole document into the memore to traverse. It will create lot of performance issues in large environment.
So in such cases if we use XMLReader or XMLTextReader objects, for 2GB document will take only few KB's in memory.
Comparison from Microsoft

Monday, October 6, 2008

Distinct of Data based on a particular Column-TSQL

When we want a distinct of data from a huge DB table (In my case I have 2 lakhs records (0.2 million))

Here ColA has duplicate records based on the ColB data.
We want distinct of ColA and one ColB data. We cannot use the following query.
SELECT DISTINCT ColA, ColB FROM tableA.
Else we have to separately store the distinct values in a DS and then loop through the table again for ColB.
Instead we can use the simple GROUP BY clause
"SELECT tableA.ColA,min([tableA.ColB])FROM tableA GROUP BY [tableA.ColA]"

Load Balancing Web Applications

Some good articles on Load balancing
1.http://www.onjava.com/pub/a/onjava/2001/09/26/load.html
2.http://www.webtechniques.com/archives/1998/05/engelschall/

Sunday, October 5, 2008

Favicon's in a SharePoint Master Page

Favicon's are 'Custom Icon for your websites'. When you hit your portal address on the browser the favicon file appears in the address bar.A Favicon is a multi-resolution image included on nearly all professional developed. Now, how do you do this in SharePoint? Here are my steps

  1. Create a image of size 16x16 using any icon creator tool.
  2. Drag a favicon.ico file to the Images directory of your MOSS site with SharePoint designer.
  3. Add the following line to your Master Page at the bottom of the head section right before the /head tag: (link rel="shortcut icon" href="/images/favicon.ico")
  4. Check-in, publish, and approve your Master Page so that anonymous folks can see the change.
  5. Refresh the site, and you should see the favicon
  6. This site will create a .ico image for you.For more details:http://www.chami.com/html-kit/services/favicon/

Tuesday, September 30, 2008

Visual Studio 2010 and .NET Framework 4.0 announced

Microsoft have provided the first look at the next version of its developer tools and platform, which will be named Visual Studio 2010 and the .NET Framework 4.0. Microsoft described the next release through the following five focus areas: riding the next-generation platform wave, inspiring developer delight, powering breakthrough departmental applications, enabling emerging trends such as cloud computing, and democratizing application life-cycle management (ALM).

Press Release

Tuesday, September 23, 2008

MVC-Model View Controller Pattern for ASP.NET

Model view controller is nothing but a design pattern used to achieve customizability in our application. Change is the only thing in the world, which will never change. All the products that we develop to our clients will undergo many changes. To accommodate these changes we should concentrate more on our design.

Good Layering Approach


MVC follows the most common approach of Layering. Layering is nothing but a logical split up of our code in to functions in different classes. This approach is well known and most accepted approach. The main advantage in this approach is re-usability of code. Good example of layering approach is UI, Business Logic, and Data Access layer. Now we need to think how we can extend this approach to give us another great advantage customizability. The answer to this is using Inheritance. Inheritance is one of the powerful concepts in oops. .Net supports this in a nice way.


View Layer


Our View /UI layer should only have UI related validations in it. We should not have any Business Logic in to it. This gives us the flexibility to change the UI at any time and we can also have different UI for different customers. We can even have web based UI for some of the clients.


Controller /Director


Controller is the layer, which responds to the events in the UI. For example Save button click on my employee master screen. This layer should act as an intermediate between our View and Model. Initially we may think that this layer is not necessary. I am also not convinced with this layer. But still we need to think. May be after some days we will get answer for this. This layer will act as just event redirector.


Model


This layer has all our business logic. This is the most important layer. This layer will have our core functionality. This layer should be designed in such a way that out core and complex logic should be designed as functions and these function should be marked as overridable so that the inheriting classes can re-use the logic or override the logic. We should not make all the functions in the layer as overridable this may raise some security threats. All the database operations should be done in the Model base class. All the inheriting classes should call this method to do database updates.


The Model-View-Controller and Model-View-Presenter Patterns


An example - Class view

Visual Studio 2008 -MVC- Template view





The ASP.NET MVC pattern interms of code is dramatically changed. Now, if we have Visual Studio.net 2008, the MVC project template is bundled with the software. For express editions we can download the templates [you must have Express editions with SP1] from Microsoft site. For more details http://www.asp.net/learn/mvc-videos/

How to run inline code in SharePoint aspx pages

How to run inline code in SharePoint aspx pages
"An error occurred during the processing of test.aspx. Code blocks are not allowed in this file".To enable this you need to modify the web.config file for your SharePoint Site Collection.Modify the web.config file

1. Open the web.config file and make the following change:


The PageParserPaths xml tags will be there already you just need to add the line in between.
2. Save web.config .

How to hide the New! icon in Sharepoint

How to hide the New! icon or control how long the icon is displayed.
The New! icon displays for 2 days by default, but you can use the command line to set that:

[C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN]\stsadm.exe -o setproperty -propertyname days-to-show-new-icon -propertyvalue [Days to Display] -url [Your Virtual Server's URL]

where SharePoint documents are stored ?

where SharePoint documents are stored ?
Exactly where SharePoint stores documents? This is not going to be some thing new for the developlers/ admin’s who used SharePoint for a while, but if you are relatively new to SharePoint it might be somewhat dazzling.
Every time you hit "Save" on a Microsoft Office document and the path for the save is a SharePoint document library on a SharePoint site, the entire contents of the document is saved in binary format in a single image-type field in the SharePoint database. That's right, the entire document, up to 2 GB in size, is stuffed completely into a single field in the SQL Server database that SharePoint uses.
For more details visit
http://msdn.microsoft.com/en-us/library/ms998690.aspx

SharePoint Server and Site Architecture Overview

SharePoint Server and Site Architecture Overview
Microsoft® Windows® SharePoint™ Services offers a highly structured server-side object model that makes it easy to access objects representing the various aspects of a SharePoint site. From higher level objects, you can drill down through the object hierarchy to obtain the object that contains the members you need to use in your code.
Depending on which part of a Windows SharePoint Services deployment that you need to customize, and the type of custom application you are creating, you can use different means of entry into the object model to obtain the appropriate higher-level object from which to start. The SPGlobalAdmin class provides access to the highest levels in the hierarchy, which are related to administrative operations, and you can also use this class to reach the lower objects in the hierarchy. Use the constructor of this class to attain an SPGlobalAdmin object to customize global administrative settings in the deployment.
If you are creating a Web Part, Web service, or Web application to work with site collections, individual sites, or lists, use the GetContextSite or GetContextWeb method of the SPControl class to attain the current site collection or site. When you create a Web application in the _layouts directory, its functionality becomes available to all sites on the Web server. Outside an HTTP context, such as in a console application, use the constructor of the SPSite class to attain a specified site collection.The following diagram shows the Windows SharePoint Services site architecture in relation to the collections and objects of the Microsoft.SharePoint and osoft.SharePoint.Administration namespaces.







1 The ContentDatabases property of the SPVirtualServer class returns an SPContentDatabaseCollection object that represents the collection of content databases used for a virtual server. Each SPContentDatabase object provides access to properties of the content database.
2 The WebServers property of the SPGlobalConfig class returns an SPWebServerCollection object representing the collection of front-end Web servers in the Windows SharePoint Services deployment. Each SPWebServer object provides access to properties of the Web server.
3 The VirtualServers property of the SPGlobalAdmin class provides access to an SPVirtualServerCollection object representing all of the virtual servers in the Windows SharePoint Services deployment. The OpenVirtualServer method of the SPGlobalAdmin class returns a specific virtual server. Each SPVirtualServer object has members that can be used to manage the virtual server. The Sites property provides access to the SPSiteCollection object representing the collection of all site collections on the virtual server, and the Add method is used to create top-level site collections.
4 Each SPSite object represents a site collection and has members that can be used to manage the site collection. The AllWebs property provides access to the SPWebCollection object that represents the collection of all sites within the site collection, including the top-level site. The OpenWeb method of the SPSite class returns a specified site.
5 Each site collection includes any number of SPWeb objects, and each object has members that can be used to manage a site, including its template and theme, but also to access files and folders on the site. The Webs property returns an SPWebCollection object representing all the subsites of a specified site, and the Lists property returns an SPListCollection object representing all the lists in the site.
6 Each SPList object has members for managing the list or for accessing items in the list. The GetItems method can be used to perform queries that return specific items. The Fields property returns an SPFieldCollection object representing all the fields, or columns, in the list, and the Items property returns an SPListItemCollection object representing all the items, or rows, in the list.
7 Each SPField object has members that contain settings for the field.
8 Each SPListItem object represents a single row in the list.




For more details vist

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

Read large text files using C#

Read large text files using C#

Reading and manipulating all the individual lines of a text file in a For Each loop isn't difficult. For example you can load all the text lines in a string array, use the array in a loop, as following
using System.IO;

using System.Text.RegularExpressions;
StreamReader sr = new StreamReader(@"c:\test.txt");
string[] tempArr = Regex.Split(sr.ReadToEnd(), @"\r\n");
// don’t forger to do house keeping for stream object using try - finally
sr.Close();
foreach ( string str in tempArr)
Console.WriteLine(str);
This approach does not work in the case of large files. Because, the above code reads all the contents into the memory. For lager files (say 20MB or higher,) the program may crash or intensively slow depends on your processor / RAM speed. We can solve this memory problem and still use the For Each loop by creating our custom class TextFileReader class that implements the dotnet’s IEnumerable interface. The interface has only one method GetEnumerator, which is expected to return an instance of another class, which must implement the IEnumerator interface.

In dotnet 2.0 we can use IEnumerable interface
using System;

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
public class TextFileReader : IEnumerable, IDisposable
{ // The StreamReader object
StreamReader sr;
public TextFileReader(string path)
{ sr = new StreamReader(path); }
public void Dispose()
{ // close the file stream
if (sr != null)
{ sr.Close(); }
}
// the IEnumerable interface
public IEnumerator GetEnumerator()
{
while ( sr.Peek() != -1 )
{
yield return sr.ReadLine();
// The "yield" keyword is used to iterate through objects returned by a method.

It creates a state engine in IL so we can create methods that retain their state and we no need to

maintain the state in code.
}

Dispose();
}
IEnumerator IEnumerable.GetEnumerator()
{ return GetEnumerator(); }
}
We can use the TextFileReader object as followes.
TextFileReader TFR = new TextFileReader(@"c:\test.txt");

foreach (string str in TFR)
Console.WriteLine(str);
Happy coding.

-Rajesh Pillai