Monthly Archives: September 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.

Advertisements

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.

To read complete post and source code please visit dotnetcoderoom.blogspot.com


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.


Show line breakes in Asp.net Label

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

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

Example:
lblDescription.Text=”name <br>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 “<br>” 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.

For demo source code visit
http://dotnetcoderoom.blogspot.com/2008/09/show-line-breakes-in-aspnet-label.html


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.


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.


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.

For complete source code visit
http://dotnetcoderoom.blogspot.com