Compare Strings in C#

by Edward 25 September 2009 05:55

Comparing string values can be very frustrating as you would sometimes need to check for NULL values, the correct case, and empty strings. Because "string" can be NULL, one should always try avoiding the equality symbol. You should rather use the static String.Compare method. This method compare strings ignoring case(which is good for comparing user input), and compare strings using a specific culture. It can also handle NULL string references, which makes it the first choise method to compare strings.

For Example:


            string string1 = "DasCode.Net";
            string string2 = "DasCode";

            if (String.Compare(string1, string2) == 0)
            {
                // if true
                Response.Write("true");
            }
            else
            {
                // if false
                Response.Write("false");
            }

String.Compare also have several overload methods, which you can read more about here.

Tags: , , ,

ASP.NET

Microsoft announces the WebsiteSpark Program

by Edward 24 September 2009 14:10

Visibility, support and software for professional Web Developers and Designers - at no upfront cost!

Scott Guthrie from Microsoft today announced the WebsiteSpark Program, a program that encourages independent web developers or web development companies that build web applications on behalf of others. This program enables you to use strategic and sometimes expensive software from Microsoft for FREE, thus at no cost, for up to three years.

Here is a list of software you can enrol for:

  1. 3 licenses of Visual Studio 2008 Professional Edition
  2. 1 license of Expression Studio 3 (which includes Expression Blend, Sketchflow, and Web)
  3. 2 licenses of Expression Web 3
  4. 4 processor licenses of Windows Web Server 2008 R2
  5. 4 processor licenses of SQL Server 2008 Web Edition
  6. DotNetPanel control panel (enabling easy remote/hosted management of your servers)

The only two requirements to join the program are:

  •    Your company(or yourself if self-employed) builds web sites and web application on behalf of others.
  •    Your company currently has less than 10 employees.

For more information on this program, visit Scott's blog entry, or the program website.

Tags: , ,

Development Resources | Technology

How to disable the mouse right-click menu

by Edward 23 September 2009 05:35

There are numerous javascript functions that allows you to disable the mouse right click functionality. However JQuery allows you to make it easier.

Here is an JQuery code example:

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        return false;
    });
});

Tags: ,

AJAX/JQuery

C# 4.0 - Response.RedirectPermanent

by Edward 21 September 2009 13:48

As discussed in an earlier post, ASP.NET 4.0 introduces some SEO improvements, to build SEO friendly sites, or make sites "more friendly" to search engines. A new feature available in the HttpResponse Object, is the "Response.RedirectPermanent" response.

If you were to use "Response.Redirect()" to direct from one page to another (Page 1 to Page 2), the search engine will keep the first page in their index. In case the original page (Page 1) no longer exists, and you want Page  to replace Page 1, you need to indicate it with a Response Code "301", and that is why Response.RedirectPermanent() method. This response sends out a 301 response message, which indicates that the Page has moved permanently. Response.RedirectPermanent() also has an overload with one parameter, incase you need to use it.

Here is a code example:

     public static void RedirectPermanent(this HttpResponse Response, string PathUrl) 
     { 
         Response.Clear(); 
         Response.Status = "301 Moved Permanently"; 
         Response.RedirectLocation = PathUrl; 
         Response.End();    
     } 

Tags: , , , ,

ASP.NET | Technology

The ?? operator in C#

by Edward 18 September 2009 05:50

The "??" operator returns the left-hand operand if it is NOT NULL, otherwise it will return the right operand.

For example:

    int? i = null;
   
    // y = i, unless i is null, in which case y = -1.
    int y = i ?? -1;

The ?? operator also works with reference types:

    //message = param, unless param is null
    //in which case message = "No message"
    string message = param ?? "No message";

Tags: , , ,

ASP.NET

Disable the Submit button after clicked

by Edward 17 September 2009 06:40

When you want to disable a button after it has been clicked, a quick and easy way to do this is to append the "OnClientClick" event handler.

For Example:

btnSubmit.OnClientClick = ClientScript.GetPostBackEventReference(btnSubmit, "") + "; this.value='Submit complete'; this.disabled = true;";

As a quick note, this will not work for buttons that causes validation of a page.

Tags: , , , ,

ASP.NET

Checking for a match in a List collection

by Edward 14 September 2009 08:04

Sometimes you would like to see if an item exist in a list collection, for example in a list containing string values.

Here is an example:

List<string> lstAlpha = new List<string>();
lstAlpha .Add("A");
lstAlpha .Add("B");
lstAlpha .Add("C");
lstAlpha .Add("D");

//prints "true" if "A" is in the list
Debug.WriteLine(lstAlpha .Exists(delegate(string searchterm) { return searchterm == "A"; }));
 

//prnts "false" as "Z" is not in the list
Debug.WriteLine(lstAlpha .Exists(delegate(string searchterm) ) { return searchterm == "Z"; }));

Tags: , , , ,

ASP.NET

Quick and Easy C# Project Documentation in Visual Studio

by Edward 09 September 2009 07:45

Visual Studio comes with a quick and easy "code documentation generator", however sometimes you might want to make the documentation look professional and presentable.

There is a few "ASP.NET code documentation generators" out there, but two of my favourites is NDoc and Sandcastle(Microsoft).

NDoc is a code documentation generator for the Common Language Infrastructure. It is licensed under the GNU General Public License.

Sandcastle is a code documentation generator from Microsoft that automatically produces MSDN style reference documentation out of reflection information of the .NET assemblies and XML documentation comments found in the source code of these assemblies.

To quickly create XML documentation for your .NET classes during every build, open the project Properties, click the Build tab, scroll down to the Output section, and check the "XML Documentation File" property. Provide a file name to which Visual Studio should save the output, and then close the dialog. Visual Studio will update the documentation every time you build. All done with the click of a button, and also very handy when you need to document code.

Visual Studio Project window

You can feed the generated XML documentation to NDoc or Sandcastle to create more professional-looking help files.

Tags: , , , ,

ASP.NET | Development Resources | Other

About DasCode.Net

I'm a ASP.NET web developer and code enthusiast. Blogging about everything .Net related.

Code... that's .net

Month List