Monday, January 19, 2009

Long-Running Operations support in Web Applications

Long running operations are most of the time a heck in web applications i.e. completing an online checkout process or sending a newsletter to hundreds of your users from your website. Here is how you can do that. An article from MSDN Magazine will help you understand the detailed process of managing long running operations.

Web Apps That Support Long-Running Operations

Sunday, January 18, 2009

Changing the trust level of a web application

Asp.net (1.1 & 2.0) web applications run under full trust, by default. It means they can access any resources; however, to limit this resource access you need set the trust level.

< level="Full|High|Medium|Low|Minimal"/>


And this is set in system.web section of the web.config file in asp.net web application.

Here is a detailed article from MSDN to elaborate the process of setting trust level, and also Code Access Security.

How To: Use Code Access Security in ASP.NET 2.0

Thursday, January 08, 2009

Using partial classes in C# and .Net 2.0

Partial classes can be used to split the class definition in multiple files and then use the methods from all of those files as single class. For example you have a class Module that contains all the properties and methods regarding the Module functionality of your project. You can split the functionality into two different classes i.e. ModuleMain.cs, ModuleExtended.cs

Here is the sample code:

file: ModuleMain.cs

code:

public partial class Module
{
//constructor
public Module() {}


//some methods go here
public bool MainMethod()
{
return true;
}

}

file: ModuleExtended.css

code:

public partial class Module
{
//extended methods go here
public bool ExtendedMethod()
{
return true;
}
}

Now, when you'll access the class Module from your code, you'll see that the intellisense will show all the methods from both of these files.

Sometimes, this feature comes very handy. For example, I wrote a utility to generate code for asp.net application. But the problem was that the classes generated by the utility would overwrite the additional methods added manually. So, I split the functionality using partial classes and separated the generated and manual code into two different files. I hope this feature will help you too.

Wednesday, January 07, 2009

Adding intellisense comments in C# using XML

We usually create classes and methods in Visual Studio 2005 using C# or VB.Net. Once we have created a class and all of it's methods and properties, we can access the list of all of them in the intellisense when we use the class in our code. The intellisense feature automatically shows us the list of public methods and properties.

However, if we also want to add some comments to explain what each method does and what are the parameters etc. we can do that using XML comments.

To add XML comments just do the following:

C#: enter three forward slashes like this ///
VB.NET: enter three single quotes like this '''

This will authomatically add a template for you as shown below:

/// < *summary >
/// /// < / summary >
/// < *param name="Name">< / param >
/// < *param name="Password">< / param >
/// < *returns>< / returns >

You can fill in appropriate information in the template as shown below:

/// < *summary >
/// This is just a test method, to check intellisense comments
/// < / summary >
/// < *param name="Name">The name of the user as string < / param >
/// < *param name="Password">The password of the user as string< / param >
/// < *returns >true if valid, flase if invalid < / returns >
public static bool TestMethod(string Name, string Password)
{
return true;
}

Now, when you'll access the method TestMethod(), you'll also see the comments in the intellisense.


Note: I have added extra spaces and a star in the xml tags otherwise blogger will block that part of the code, so you can remove the spaces and stars while using the code.

Tuesday, January 06, 2009

User not associated with trusted SQL Server connection

Sometimes if you try to login to the sql server using sql server authentication you receive an error message saying "Not associated with trusted sql server connection". It means that the sql server authentication mode is not activated for this insatance of the sql server. To enable both windows authentication and sql server authentication modes for a particular instance of the sql server do the following:

1. Connect to the sql server using windows authentication mode i.e. Windows credentials
2. Open SQL Server Enterprise Manager
3. Expand SQL Server Group in the object explorer
4. Right click on the instance of the sql server for which you want to enable sql server authentication
5. Right click on the instance and then click properties
6. Click security tab on properties window
7. Now click "SQL Server and Windows authentication mode"
8. Click OK
9. Restart sql server
10. Login using SQL Server credentials

This is gonna work!