Monday, September 29, 2008

Preload images in HTML using JavaScript

Preloading means loading an object (graphics, text, movie) before showing it to output.

A good example is of preloading in flash files, when you see a loader before displaying the content, and after loading site shows a smooth experience of flow of data.

This is mostly used when there is an mouser over images to be loaded. If user mouse overs an image and then you loads the new mouse-over image it may take a few seconds to load, which leads to a small blank image over the image area. By pre-loading the image you can show mouseover image instantly without any delay.

In this post I am explaining how to do preaload imags in HTML using JavaScript, without using flash.

We have to use the Head tag of html as this is the one which loads on first page executiution.

<head>

<script type=”text/JavaScript”>

if (document.images)
{
pic1= new Image(190,53);
pic1.src=”images/productSmall.gif”;

pic2= new Image(190,53);
pic2.src=”images/storeSmall.gif”;
}

</script>

</head>

The above code lods two images naming productSmall.gif and storeSmall.gif respictively from images directory. It creates a new object of image and assigns it a dimention and a file to load.

The document.imags condition has been used to determine that the browser supports the images or not.

Using RequiredFieldValidator with DropdownList Asp.Net

You may have used RequiredFieldValidator with Text controls, but if you wnat to use it with DropDownList controls, you need to do some little extra work. Here is the trick.Step 1: Create an DropDownList

Step 2: Drag and RequiredFieldValidator. Set it’s ControlToValidate property to the DropDownLis’s object.

Step 3: Fill some items in the DropDownList. Add a first item as “Select”. This will indicate the user that he should select an option.

Step 4: Now set the RequiredFieldValidator’s “InitialValue” property to “Select”.

Now all of your code should look like this :

<asp:DropDownList ID=”cmbCountry” runat=”server”>
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>Canada</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID=”rqCountry” runat=”server” ErrorMessage=”*” SetFocusOnError=”True” ControlToValidate=”cmbCountry” InitialValue=”Select”></asp:RequiredFieldValidator>

Now you are all set to go and test it.


Click here to download Source code

Wednesday, September 24, 2008

Change MS SQL 2005 login password using sql query

If you want to change the MS Sql 2005 ’s password then just run the following sql query after logging into query anlyzer

sp_password ‘oldPassword’ , newPassword’

Note
Always use strong SQL passwords (i.e. having uppercase characters , lowercase characters, numeric values, special characters) for security purpose, this way it will be difficult to hack, hard to guess, hard to crack by brute force attacks.

Wednesday, September 17, 2008

Show line breakes in Asp.net Label

To show line breaks in a server side asp.net label, you can either use a HTML formatted text or a plain text.

HTML formatted text contains “
” tags so while rendering the control .net framework handles the task to show the proper html formatted text into a Label.

Example:
lblDescription.Text=”name
description”;
The above code will display a line gap between name and description.
But in case if user inputted data using a non-HTML enabled TextBox then it is a point of problem.

While displaying data, inputted by simple multiline TextBox, line breaks looses its state and when the data displayed on a label it looks like a simple constant text line. Interestingly if you try to show the same data back in a TextBox it will look nice with proper line breaks. But we can’t use TextBox every where because of designing requirements.

To keep the line breaks you must convert the HTML new line special indicator to HTML break “
” tag.

Solution:
lblDescription.Text = txtInput.Text.ToString().Replace(Environment.NewLine, “<br>”);

The above code replaces the “NewLine” indications to “<br>” at run time, and displays the data same as input formatting.

Download Source Code

Friday, September 12, 2008

What is RSS feed

Really Simple Syndication (RSS) feeds are XML files which are provided by websites so you can view their contents on other website rather than browsing their site. Basically it’s an information sharing process having some specified
content format of text and related links.

Example:

I have a blog and you want to show the my Blog’s content on your website.
So what i have to do is to create a RSS of my blog content and give RSS generation page link to you.
The RSS page will generate an XML based on content I want to write in the file.

You have to consume that XML into your website and show the content.

These days nearly all blog websites provides their own RSS consuming applications, so you don’t have to bother about it if you are using a public web portal like orkut, WordPress, Blogspot etc.

Wait for a new post for reading and writing RSS applications.

Tuesday, September 9, 2008

Operator + cannot be applied to operands of type string and method group, C# error

Cause

This error comes up when you tries to append two strings with the “+” sign. As the + sign in C# is a arithmetical operator so you cannot use this too append two string type values. To use “+” the values or variables must be if numeric type.

As C# uses doesn’t supports implicit typecasting, for string concatenation use “&” sign.

Example

string varTest = "";
string str1 = "abc";
int str2=21;

varTest = str1 + str2;

The above stqatmet will cause in error stating "Operator + cannot be applied to operands of type string and method group". Because we are trying to add a string variable in a numeric variable.

To remove the error you must convert all the numeric (integer) datatype or variables to string.

varTest = str1 & str2.ToString();

The output of the above code will be abc21

Conclusion
We must convert all values to a uniform datatype before storing them into a variable.

Saturday, September 6, 2008

Send e-mail in ASp.Net 2.0 and onwards in C#

The following code example can be used to send email using Asp.Net in 2.0 and higher versions version.

The Namespace of email class is changed to System.Net.Mail from System.Web.Mail.

Import the System.Net.Mail Namespace
using System.Net.Mail;

Code:

MailMessage mail = new MailMessage();
mail.From = new MailAddress(”info@yourDomain.com”,”Alert”);
mail.To.Add(”you@yourdomain.com”);
mail.Subject = “mail test”;
mail.IsBodyHtml = true;
mail.Body = “Your message here”;
SmtpClient smtp = new SmtpClient(”mail.yourdomain.com”);
smtp.Credentials = new System.Net.NetworkCredential(”info@yourdomain.com”,”password of this info”);
smtp.Send(mail);

Note:
The email address specified in the “FROM” field must be valid and it need to be of your domain.
In the SmtpServer the domain name must be same as of your domain on which website is hosted.

Click here to download full source-code for this post

Send e-mail using Asp.Net 1.1

The following code example can be used to send email using Asp.Net in 1.1 version.

First import Web.Mail namespace.
using System.Web.Mail;

Now add this code to your button click event:


MailMessage mail = new MailMessage();

String msgText = string.Empty;
msgText = “Message here, you can also use Html tags for Rich Text Formatting.
Hi.”;

mail.To = “you@yourdomain.com”;
mail.CC = “anyEmailAddress, its optional”;
mail.Bcc = “anyEmailAddress, its optional”;
mail.From = @”"”Ajay”" ”;
//mail.From = “info@yourdomain.com”;
mail.Subject = “Mail test”;
mail.BodyFormat = MailFormat.Html;
mail.Body = msgText;
SmtpMail.SmtpServer = “mail.yourdomain.com”;

SmtpMail.Send(mail);

Note:

The email address specified in the “FROM” field must be valid and it need to be of your domain.
In the SmtpServer the domain name must be same as of your domain on which website is hosted.

Wednesday, September 3, 2008

Google’s Web Browser called Google Chrome is launched

Yapee.

Google has launched it’s own web browser named Google Chrome.
Google Chrome is a very simple and sophisticated browser and very easy yo use.

Google Chrome Features:
~ Multiple tabs can be opened in same instance window.
~ Thumbnails of most visited pages.
~ Automatic bookmarks and history search reaults when you type any URL.
~ Import settings and bookmarks from other web browsers.
~ Application shortcuts
~ Dynamic tabs
~ Crash control
~ Incognito mode
~ Safe browsing
~ Instant bookmarks
~ Simpler downloads

For more features visit http://www.google.com/chrome/intl/en-GB/features.html
Download Google Chrome here http://www.google.com/chrome/

So go and give a try to Google Chrome.

Monday, September 1, 2008

Rename a table Column in MS SQL

How to rename a MS SQL server database table column name after the table creation??
You can rename a column regardless of it is null or contains any data.

We need to use a System stored procedure to rename. Any command with alter keywords won’t work.

Code
EXEC sp_rename ‘tableName.[oldColumnName]‘, ‘newColumnName’, ‘COLUMN’

Example:
EXEC sp_rename ‘Ultra_tblPressRelease.[uShowStatus]‘, ‘uShowInA2B’, ‘COLUMN’

The above line will rename the existing column named uShowStatus of table tblPressRelease to uShowInA2B.

There will be no adverse effect of this procedure on your table data.
But you have to update all your SQL Cursors and Stored Procedures with the new column name (if any).