Wednesday, April 08, 2009

Overloading methods in WCF

Service oriented development is different than the Object oriented programming. Overloading methods is possible in WCF, but not recommended.

To make it work in WCF, you'll need to add Name property

[ServiceContract]
public interface ICalendarService
{
[OperationContract(Name = "GetScheduledEventsByDate")]
ScheduledEvent[] GetScheduledEvents(DateTime date);

[OperationContract(Name = "GetScheduledEventsByDateRange")]
ScheduledEvent[] GetScheduledEvents(DateTime start, DateTime end);
}

For more info: here

SQL: Return limit Rows

In the asp.net application, i found the good way to return limit rows from data table is to use the ROWCOUNT

CREATE PROCEDURE GetContacts
(
 @ModuleID int,
 @maxrows int = 0
)
AS
 SET ROWCOUNT @maxrows

 SELECT
   ItemID,
   CreatedDate,
   CreatedByUser,
   Name,
   Role,
   Email,
   Contact1,
   Contact2
 FROM
   Contacts
 WHERE
   ModuleID = @ModuleID
For more info: http://authors.aspalliance.com/stevesmith/articles/sprowcount.asp

Wednesday, April 01, 2009

Change the author initial when review comments

  1. On the Review tab, in the Tracking group, click the arrow next to Track Changes, and then click Change User Name.
  2. Click Popular.
  3. Under Personalize your copy of Office, change the name or initials that you want to use in your own comments.

Thursday, March 12, 2009

FxCop: exclude the naming rule for some words

I used the Iframe identifier in my project and FxCop did not like it.

The way to get around this without rename everywhere is to include a custom dictionary named CustomDictionary.xml in the project directory.


for more info: http://msdn.microsoft.com/en-us/bb264492.aspx

Friday, March 06, 2009

FxCop to Excluded in Source

There are 2 ways to make the rule exceptions in FxCop. One way is to update the .fxcop file. Another way is to update your source code.


For Excluded in Source, add add the following line on top of the method/function/data members.

[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Need to set Specialties collection")]

Also, need to declare System.Diagnostics.CodeAnalysis in your source file.

using System.Diagnostics.CodeAnalysis;

Wednesday, January 28, 2009

StringComparison

Here is a great article on when to use the StringComparison.

http://msdn.microsoft.com/en-us/library/ms973919.aspx

DO: Use StringComparison.Ordinal or OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.

DO: Use StringComparison.Ordinal and OrdinalIgnoreCase comparisons for increased speed.

DO: Use StringComparison.CurrentCulture-based string operations when displaying the output to the user.

DO: Switch current use of string operations based on the invariant culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is linguistically irrelevant (symbolic, for example).

DO: Use ToUpperInvariant rather than ToLowerInvariant when normalizing strings for comparison.

DON'T: Use overloads for string operations that don't explicitly or implicitly specify the string comparison mechanism.

DON'T: Use StringComparison.InvariantCulture-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data.

Thursday, January 22, 2009

Use delegate to sort the generic list (collection)

Here is an example of how to use the delegate to sort the list:

returnList.Sort(delegate(IMyClass x, IMyClass y)
{
return String.Compare(x.Name, y.Name);
});

So, instead of override the ICompare, you can use this short-cut to return a sorted list.

Here is how to just make your Thing class implement IComparable, implementing the CompareTo method like this:

public int CompareTo(Thing other)
{
return Name.CompareTo(other.Name);
}

Monday, December 08, 2008

Format data in gridview

To format the date and time, use DataFormatString="{0:MM/dd/yyyy hh:mm tt}"

d = day of month without leading zero
M = month without leading zero
yyyy = year as four digits
h = hours in 12 hour format without leading zero
mm = minutes with leading zero
tt = two character am/pm designaror

To format the "%", use DataFormatString="{0:0%}"

To format the currency "$xx.xx", use DataFormatString="{0:C}"

Tuesday, December 02, 2008

Validate image size, type, and dimension

When upload images to file server or database, most likely we'll put some restrictions on the size, dimensions, and type of image.

In my asp.net project, I used the custom validator to accomplish this goal. In the validator function, we can check the size, dimension, and type. Make sure to use Page.IsValid in order to show the error messages.

    protected void valImage_ServerValidate(object source, ServerValidateEventArgs args)
{
int maxSize = 1024 * 1024;
int maxWidth = 350;
int maxHeight = 225;

if (args.IsValid)
{
if (FileUploadImage.PostedFile.ContentLength > maxSize)
{
args.IsValid = false;
CustomValidatorFile.ErrorMessage = "The image file size must be less than 1 MB.";
}
else if (FileUploadImage.PostedFile.ContentType != "image/jpeg" &&
FileUploadImage.PostedFile.ContentType != "image/pjpeg")
{
args.IsValid = false;
CustomValidatorFile.ErrorMessage = "The image file type must be jpeg.";
}
else
{
GetNewImage(); //put image stream in the session state;
using (Bitmap bitmap = new Bitmap(FileUploadImage.PostedFile.InputStream, false))
{
if (bitmap.Width > maxWidth || bitmap.Height > maxHeight)
{
args.IsValid = false;
CustomValidatorFile.ErrorMessage = "The image dimension must be less than 225px in height and 300px in width.";
}
}
}
}
}


reference: http://aspalliance.com/781_CodeSnip_Validate_Image_Size_Dimension_and_Type_Uploads;
http://www.mikeborozdin.com/post/ASPNET-Image-Uploading-(part-I).aspx

Friday, November 21, 2008

Centering: Auto-width Margins

By setting its right and left margin widths to "auto", it will make the content box horizontally centered. This is the preferred way to accomplish horizontal centering with CSS, and works very well in most browsers with CSS2 support.

Unfortunately, IE5/Win does not respond to this method. The workaround is to set the text-align to center.

Margin example:

h1 {margin: 10px}
all four margins will be 10px

h1 {margin: 10px 2%}
top and bottom margin will be 10px, left and right margin will be 2% of the total width of the document.

h1 {margin: 10px 2% -10px}
top margin will be 10px, left and right margin will be 2% of the total width of the document, bottom margin will be -10px

h1 {margin: 10px 2% -10px auto}
top margin will be 10px, right margin will be 2% of the total width of the document, bottom margin will be -10px, left margin will be set by the browser

Thursday, November 13, 2008

Host File

The short answer is that the Hosts file is like an address book. When you type an address like www.yahoo.com into your browser, the Hosts file is consulted to see if you have the IP address, or "telephone number," for that site. If you do, then your computer will "call it" and the site will open. If not, your computer will ask your ISP's (internet service provider) computer for the phone number before it can "call" that site. Most of the time, you do not have addresses in your "address book," because you have not put any there. Therefore, most of the time your computer asks for the IP address from your ISP to find sites.

Windows NT/2000/XP Pro c:\winnt\system32\drivers\etc\hosts

For more info, go to HERE

Wednesday, November 12, 2008

Open Command Window Here

One of Microsoft's Power Toys. This PowerToy adds an "Open Command Window Here" context menu option on file system folders, giving you a quick way to open a command window (cmd.exe) pointing at the selected folder.

Launchy

Launch the application without using mouse.

Alt+Space to bring the Launchy to front. Type the name of the executable, it will show the matching list while you are typing. Press Enter when you done and application will launch. It is open source and FREE.

http://www.launchy.net

Tuesday, November 11, 2008

Address Completion in Browsers

IE: type the site address between the "www." and ".com" and then use Ctrl+Enter to auto-complete the "www." and ".com"

Sunday, November 02, 2008

SQL 2005 installation

I always run into problem with installation of SQL 2005. Here is step by step guide:

http://sequelserver.blogspot.com/2007/06/20070703sql-server-2005-installation.html

Tuesday, October 28, 2008

Essential Visual Studio Tips & Tricks that Every Developer Should Know

Found this great post on Stephen Walther's blog.

tip 1: don't need to select a line to copy or delete it

If you want to copy a line of code then you can simply press CTRL-c to copy the line and press CTRL-v to paste the line. If you want to delete a line, don’t select it first, just press CTRL-x. You’ll be surprised how much time this one tip will save you.

tip 3: Never create properties by hand

just type prop + TAB + TAB. When you type prop + TAB + TAB, you get a code snippet (template) for entering a property. Use TAB to move between the template parameters. Press the ENTER key when you are finished creating the propert.

I found these two are most useful and time saving for me. Others are at: http://weblogs.asp.net/stephenwalther/archive/2008/10/21/essential-visual-studio-tips-amp-tricks-that-every-developer-should-know.aspx

Monday, October 27, 2008

Jing Project: Visual conversation starts here.

Jing is a great screen capture tool and it's free!! This is so far the best screen capture tool I used. Another feature is it also can record your activity, so it also a great tool to create the video demo.

www.jingproject.com

XNView

Photo viewing tool. like ACDSee, but it is FREE.
NView

Tuesday, October 21, 2008

Take dnn site offline

It's better to take the DNN site offline for maintenance. The best way to do this is to upload an App_Offline.htm file to the root.

Monday, October 06, 2008

Navigate the parent page from the IFrame page (child page)

When user clicks the link/button on a page inside the IFrame, it will bring up another page inside the IFrame and the parent page will not change.

Sometimes we do need the link/button on the Iframe page update the parent page. Here is how to do it.

If the link is used, make the target property "_top".
HyperLink

If the button is used, in the code-behind, do this:
Response.Write("<" + "script>" + "window.open('http://webpage_address','_top');<" + (char)47 + "script>");