Saturday, October 18, 2008

SQL Server 2008 Alter Table - Saving Changes Not Permitted ???

Do you get this error and freak out?






























Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.




LETS NOT GET INTO WHY IT IS LIKE THIS :)




Do this instead




Go to tools - Options and do as pointed out. - Thanks























Monday, October 13, 2008

T-SQL - If the Day is week day OR week end

Many Application needs this so trying to post this.

( I am not the author of this bu thought to help all by posting it here)

create function fn_IsWeekDay
(
@date datetime
)
returns bit
as
begin
declare @dtfirst int
declare @dtweek int
declare @iswkday bit
set @dtfirst = @@datefirst - 1
set @dtweek = datepart(weekday, @date) - 1
if (@dtfirst + @dtweek) % 7 not in (5, 6)
set @iswkday = 1 --business day
else
set @iswkday = 0 --weekend
return @iswkday
end


Short and sweet.

Now you can simply do this: (USAGE)


if dbo.fn_IsWeekDay(@date) = 1
begin
--do some magic here ;-)
end
--or
select a.SomeFieldsForCalculation from table a where dbo.fn_IsWeekDay(a.SomeDateField) = 1



and all is good.

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