Thursday, September 25, 2008

Simple Memory Management Stuff.


I belive that Once the code is written it lives for ever. Write it best for the first time. No One lakies to refactor it.
consider yourself fortunate if you are getting opportunity to wrte the code from scratch ang follow these simple things.
More to come

Monday, September 22, 2008

UNICEF - Be a member. It really helps

Hello,

Here I am to request who ever read this post. We all know that there are some among us that cant even think of looking at computer. I really feel bad about those who are disabled and its not their fault.

I have joined UNICEF. Its my urge to you all to join.

It doesn't take that much from your life an pocket.

http://www.unicef.org/

thanks

Thursday, September 18, 2008

How can I maintain the format of text in a multiline textbox when saving to sql server ie new lines paragraphs etc in ASP.NET

Very common scenario - Here are my Thoughts

1. If you are using multiline text box (built in asp.net) - You can only save the New lines.

here is a quick c# code.

string FinalString = MultilineTextBox.Text.Replace("\r\n","");

and then save it in regular fashion in SQL server. Make sure to turn off the page validation request. i.e. ValidateRequest="false" at page level.

-------------------------------------------------------------------------------

2. Very Elgant, Less time consuming, and FREE way to do it.

a. Go to Free Text Box and download.
b. Put the DLL in Bin of your website.
c. Add a tag in your page.

<FTB:FreeTextBox id="txtDesc"
ToolbarLayout="ParagraphMenu, FontFacesMenu, FontSizesMenu, FontForeColorsMenu,
FontForeColorPicker, FontBackColorsMenu, FontBackColorPicker, Bold, Italic, runat="Server" Width="800px"/
>


d. This is server side control and directly give you HTML when you acesses the Text property in code behind. Save it in the SQL server as nvarchar and you are good to go.

thanks.

(Leave a comment if this helped you)

Validating Image File for Content Type with ASP.NET FileUpload web control.

We use file upload web control in our sites, where we want only valid image types to be uploaded.
Without talking much -> here are 3 ways to do it.

1. Accept attribute of fileUpload control - Doesnt work with most of the browsers (Bad with IE itself)

2. Regular Expression validator for FileUpload



This is Not very handy when using AJAX controls on page- Both javascripts dont like each other :)

3. Write a code behind function in a very simple way - Most robust way to use it. - Very Generic can be used for any file types.

C# code
private bool IsFileValid()
{
string [] allowedImageTyps = {"image/gif", "image/pjpeg", "image/bmp","image/x- png"};
StringCollection imageTypes = new StringCollection();
imageTypes.AddRange(allowedImageTyps);
if (imageTypes.Contains(FileUpload1.PostedFile.ContentType))
return true;
else
{
return false;
}
}

NOTE - use FileUpload1.PostedFile.ContentType and not the file name to check the Extension

Leave a comment if this helped you