How to control Authorization Permissions in an ASP.NET Application

Hi

When we use the forms based Authentication in Asp.Net 2.0 Application. Only the authenticated users can access pages in the application. Unauthenticated users are redirected to the specified login page provided by the loginURL tag. If the user login from that page then the user is redirected to page they wanted to go.

This is perfect when all the pages in the web site are login protected. But that’s not true with all the application. In most of the application some pages are login protected but many pages are not login protected. This means that the user should be given access to a given number of pages even if the user is not authenticated and in login protected pages the user should not allowed the access.

Continued...

Working with MultiView control in Asp.Net 2.0 to show the same page with different views

Hi

Data collection is a common task in web application. We might have to collect data for survey and display result or use the contact us form and after the form is submitted show the user a response that the information has been submitted.

Most the time people transfer these into multiple pages so that the user does not have to see a real long page, and feel that he has to input too much of data.

Many a times in Asp.Net 1.X developer used to complete this task in one form by using multiple panels. They would hide and unhide panels (having a set of controls). This was easier then to create multiple pages for the job.

In Asp.Net we have the MultiView control to work around this problem. This is a new control in Asp.Net 2.0. MultiView makes life easier for developer when they have to show one view at a time out of many views in the same page.

Continued...

How to easily encode and decode a string in Base64 format

Hi

Yesterday I had a task where I had to encode some string in the Base64 format and then later decode the same back to string. When I first started looking at the System.Convert class, I found the convert to base64 function does not take any string parameter.

You can still convert a string into base64 format in one line of code. The code to convert a string to base64 format is

string str64;
string str64back;
string str="Vikram"; 

Continued...

How to send asynchronous emails in Asp.Net 2.0
Hi

Another of the new feature in asp.net 2.0 is the support to send emails asynchronously. This is a very important feature. With the help of this feature you don’t need to wait for the email to be sent before performing other tasks in the page. But instead these tasks can be performed while the mail is being sent asynchronously.

To send Emails asynchronously we need to wire up a sendComplete event, create a send complete event and then call the sendAsync event.

Continued...

How to send an embedded image in the Email in Asp.Net 2.0
Hi
Another of the new feature of the Asp.Net 2.0 in the Emailing section is embedded images. With System.Net.Mail we can now embed images in the Emails sent from Asp.Net 2.0 web site.
To send an embedded image in the mail we first need to create an object of LinkedResource. This linkResource will actually contain the binary data of the Image. This binary data is then encoded as a part of the image and sent along as a part of the mail message.
We also need to give the LinkedResource a unique name and a content ID. We then create an HTML Alternate view. Inside that Html text we can use the standard img tag. The src attribute value will be the content
                
Continued...

How to get the child hierarchy for a given category in a self referencing table for delete hierarchy of category

Hi

While working with a self referencing table when we need to delete a record, we also need to delete all the records inside that category. Hence the delete should have a cascading effect on the records.

For this reason its best to create a self referencing function which will give back a table having all the child values. This function then can be used to get the entire child category list and delete them with and in statement.

For our example we are using a Self referencing NLevelCategry table which is like this

Continued...

How to get the parent hierarchy for a given category in a self referencing table

Hi

If you have a Self Referencing Table one of the major tasks is to get the parents of the given category. This will be required when you want to show the bread-crumb, and many other things.

For this reason its best to create a self referencing function which will give back a table having all the parent values. This function then can be used to get the entire parent category and work with it.

For our example we are using a Self referencing NLevelCategry table which is like this

Continued...

What are the things that can be controlled in the HttpRuntime element in System.web element in the Web.Config file?

Hi

In Asp.Net 2.0 we can set many property related to the working of the application. We can set the Execution time out, upload file size etc for the application. We need to set these properties in the httpRuntime section, which is under <system.web>, which will be under <configuration> section.

<configuration>
<system.web>
<httpRuntime>
</httpRuntime>

</system.web>

Continued...

Sending Email with attachment and creating attachments from memory stream in Asp.Net 2.0
Hi
Many a times we have to send Emails with attachment. In Asp.Net 2.0. We can add as many attachments to the mail as we want. To add a attachment to the site use the following code. 
MailMessage.Attachments.Add(new Attachment("c:\\test\\Vikram.txt"));
To add more attachments simply go on adding attachments to the mail class by using the Add method
Remember to send the attachment Asp.Net process of the impersonated account will need to have the permission to read and attach the file to the MailMessage
                
Continued...

How to create default value for property in Dot Net

Hi

Many a times when we create a property we want to assign it a default value. We
will have to use the namespace System.ComponentModel for this.  We assign the default value with the help of the attribute DefaultValue.

Here is how you do that in C#


using System.ComponentModel;

public class c1

    {

Continued...

How to send mail in Asp.Net 2.0 so that we get a mail notification when the user reads the mail for the first time.

Hi

Many a time we want to add read receipt header to the Email we sent. A read receipt is a notification method where by an Email is sent to the given Email address when the mail is first read. To add a read receipt Notification to the Email in Asp.net we need top add the header Disposition-Notification-To in the Email. This can be done very easily by one line of code.

Continued...

How to set the priority of an Email in Asp.Net using the System.Net.Mail namespace

Hi

Many a times when we send Emails from the site we want to make it a high priority Email. In Asp.Net 2.0 the Email functionality is governed by the System.Net.Mail namespace.

The headers in the above-mentioned class control the priority of a mail. But the name space also exposes a property to set the priority of the mail. We can use the Priority property of the MailMessage class to set the priority. The priority can have one of the three values High, Low, Normal (from the MailPriority Enumeration)

Here is an Example that shows the how to change the priority of the mail to high.

Continued...

Working with function of Path class in System.IO Namespace

Hi

The path class of the System.IO namespace is very useful to work with file paths. This class contains many static properties to work with file path. Some of the functions of the class are listed below

Path.Combine(string, string)

This function combines 2 paths together and returns a singe path. This function is very useful if we have directory path and the file path differently. In that case we can use the function to combine them to a single path. A simple concatenation can give us a problem with whether to put the “/” string or not. But with this function we do not need to test these conditions.

Path.GetExtension(string)

Continued...

Why does most of the Email fired from an ASP.NET Site go to the Spam folder?

Hi

Does the Email Sent from you site built in ASP.NET go in the Spam filters of most of the client? Its been happening with me a lot. Most of the Client complains about not getting the Email to finally realize that the mail has gone to their Spam folder.

Of late I have been working with the new System.Net.Mail class for sending emails from the Site. I found that all the mail sent from the Site had their headers added in lower case. This problem was not there in Asp.Net 1.1 when I used the System.Web.Mail class

Continued...

Events fired during the Life Cycle of the of a page in Asp.Net 2.0

Hi

A few days back I talked about the event in the Asp.Net application Life cycle. It talked about the entire event that takes place when a request is made. You can read the article here. In this Article I am going to talk about the event that takes place in the Page.

PreInit()

In this event all the page level controls are created. The property assigned in the HTML is also assigned to the controls in this event. This event is only there for page and not present in user control (Hence not present in Master Page also). If we want to change the Theme and master page of a page, we need to override this event.

[Note: When you override this event remember to call the base pages preInit command.]

 

Continued...

Added a new theme to the site after a long period of time

Hi

So after a long period of time we again have a new theme for the site. This one is called Candle Light. This one took quite some time.

You can change the theme of the site by using the Change the Theme button on the left. It will give u a list of theme and you need to select a theme and that will be loaded.

Continued...

Added an Image Search to site using the Flickr API

Hi

Today I have added another new functionality to the Site. Now the site also has an Image search feature. The Link in the top to find image takes you to the images search page. I am using the Flickr API to search Image. After searching the Image I load them.

When you hover over an image you will see the enlarged version of the Image. This was done with the use of Ajax Extension. I am using the Popup extender to display the enlarged image when the user hovers over the image.

I am not a user of Flickr.com of you will not find any image of mine when you search with my name but hope you find some interesting ones in the image search page.

Hope this helps
Thanks
Vikram


Giving the site a new Identity by giving a new Icon

Hi

After quite a few days I have added anew feature to the Site. A favicon for the Website. The Icon gives an Identity to the site. A favicon is that little icon beside a URL in the address bar of your browser.

You can already see the in the Title and address bar. It was fairly simple. Create an .ICO file. The ico file will be the icon. Add a small code to the header of the site. As I am using the Master page this was fairly easy. All I had to do was add a tag in the HEAD section of the master page.

<link REL="SHORTCUT ICON" HREF="iconName.ico">

The size of the icon should be 16 * 16 and should be saved (you should be keeping the icon file) in the root of the application.

He Icon also works with IE7 ad Firefox. Hence no problem with different browser also

Continued...

what went wrong with my site???

Hi

Finally it's over!! The last 16 hrs had been no less than a nightmare for me. Last night, as I was wrapping up my day on a positive note with one new theme almost ready to be uploaded, I realized that my site was not available online. Immediately called up the so-called 24x7 customer service of my providers. No response from them which came as a shocker to me, for they had promised me tech support round the clock.

I guess its true promises are made to be broken. Anyway, my sleep was ruined, and the night passed with me tossing on my bed. My day began with me constantly calling up these useless people and finally they did respond to me.

Continued...

How to insert multiple records in a table using a stored procedure

Hi

Many a times we have to insert multiple data on click of one button. Like inserting all the checked records in the favorite table of the member. It is not a good idea to insert the records by using as many insert queries.

At these points we should insert all the records with only one call to the database. We can use a stored procedure that takes a comma (or any separator you want to use) list of values and insert them in the database. This way the whole process will be very fast and efficient.

In this example we will insert data in the MemberProductfaveriote table. We will be insert the member ID and the MemberProductId(all the product selected as favorite from the list). We will pass the MemberID and the comma separated list of MemberProductId.

The stored procedure will insert each of the record in the database.

Continued...

Performing separate validation scenarios on the same page in Asp.Net

Hi

Earlier in asp.net 1.1 when we wanted top validate only a few fields on the click of a button and a few fields in the click of another button on the same page, there was no way to this using the validation control of Asp.Net.

In Asp.Net 2.0 this problem is solved. Now when we want to perform separate validation scenarios on the same page we can use the validationGroup property. With the help of this property we can group the validation control and then tell the post back control to validate a particular group only.

This is very useful for the editing scenarios of the data controls. We need to set the ValidationGroup property to the same value for all the validators that belong to a particular group. On the button control or the postback control (with causes validation property set to true) set the validationGroup property to the name of the group that the control needs to validate when there is a postback.

Continued...

Use ValidateEmptyText property of the custom validators to validate even if the control to validate is empty

Hi

In Asp.Net 1.X there was a problem in using the custom validators. The custom validators never fired if the control to validate was empty. So it was not very useful in all the places. In Asp.Net 2.0 this problem has been solved.

The custom validators have a new property called ValidateEmptyText. If we set the value of this property to true then the custom validators will validate even if the control to validate was empty.

Hope this helps
Thanks
Vikram


Attributes for configuring Forms authentication in Asp.Net

Hi

Many a time we use the forms authentication in our application. When using the forms authentication we need to configure it in the web.config file. There are many attributes to the configuration, which are many a time not utilized properly. Here is a look at the attributes of the Forms Authentication.

[Note: remember Forms element is the sub element of the authentication element inside System.web element in the web.config]

Continued...

How and where to use the Image Map control in Asp.Net

Hi

When we want to work with hot spot regions in an image, we can use the ImageMap control. We can define the Hotspots regions for the image and when the user clicks the Hot spot region, the control can either generate a post back to the server or go to a specified URL.

If we use the control to display a map of geographical region and the use clicks a specific region on the map, the control can navigate to the URL that will provide the additional data about the region clicked. This control is also very useful if we want to capture the response of the user for the votes (the region user clicks for the vote).

We use the top, left, bottom and right property to define the location of the hot spot. We need to provide the hotspot mode property to define what will this hotspot do (navigate or postback). We use these properties inside the hot spot template (like asp:RectangleHotSpot

Continued...

How to get Random records from the database

Hi

Many a time we want to get random number in SQL server. There is an easy way to do this. Use the RAND function to generate a random number. Here is an example of the RAND Function

SELECT RAND()
Returns result 0.5144366554661576

What if we want a random number with each record in the result set. But the RAND function will give you same value if used (as a column) in a query. This is not what you want. So what is the solution?

We can create user-defined function that returns the Random values. This way we will get a random number for each record in the record-set.

We can also use the NEWID() function. The NEWID function returns a unique identifier for each row. The syntax to use this would be like

Continued...

Using the SqlCommand.close method before using datatreader.close method

Hi

The other day I was reading about the data reader class in Dot Net. It is highly said that data reader is faster than the dataset. So we should use the data reader when working with the read-only data. Although this statement is true but there are a few other things that should be taken into notice when working with data reader.

There is one problem with the data reader. Even if you call the close method of the data reader class the data reader will retrieve data from the stream and then close. This can have some performance issue. If there is a big amount of data in the stream, it can take long time and might cause a timeout error. To solve this always use the Cancel method of the SQL Command you are using. The better thing would be to restrict the result set to be smaller if it is not supposed to be used.

Hope this helps
Thanks
Vikram


When and why to use the truncate and the delete command

Hi

In SQL server 2000 there are 2 main keywords for deleting data – Truncate and delete. Although the end result might be same but both work very differently. We should take into consideration the advantages, limitation and the consequences when using one of them.

When we use the delete statement, SQL server deletes one row at a time. Each row is logged in the transaction LOG. This also means theta the server will also maintain the Log Sequence number. This will consume more database more resource in the database and the process will be slow. But this also gives an advantage. The transaction can be rolled back as there are transaction log.

Also you can use the where clause with the delete command but not with the truncate command. With truncate command it’s all records or nothing. Also one more advantage of the truncate command is that it also resets the identity seed of the table. Also the fact that deallocated pages is returned to the system for use in other areas.

Continued...
 
Copyright © 2006 - 2010 Vikram Lakhotia