Showing posts with label Performance Tips. Show all posts
Showing posts with label Performance Tips. Show all posts

Wednesday, March 25, 2009

WCF Performance: Making your service run 3 times faster

Wisely changing WCF defaults can yield a significant improvement in your service performance. The exact changes need to be made and their exact effect are dependent in the scenario. The example below how to speed up a certain service 3 times faster.
Read more at http://webservices20.blogspot.com/2009/01/wcf-performance-gearing-up-your-service.

Happy Coding!

Thursday, December 25, 2008

C# array in descending or reverse order

How do you sort a C# array in descending or reverse order? A simple way is to sort the array in ascending order, then reverse it:
int[] intArray = new int[] { 3, 1, 4, 5, 2 };
Array.Sort<int>( intArray );
Array.Reverse( intArray );
Of course, this is not efficient for large arrays.

A better approach is to create a custom Comparer. Following is a nice generics class that will sort an array in descending order. Note that the object type must be comparable (inherit from IComparable) as any useful class should.

Here’s a simple web application to test it:

<%@ Page Language="C#" AutoEventWireup="true" Codebehind="SortSample.aspx.cs" Inherits="SampleApplication.SortSample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sort Ascending /Descending Order by Generic</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace SampleApplication.
{
public partial class SortSample: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] intArray = new int[] { 3, 1, 4, 5, 2 };
char[] charArray = new char[] { 'r', '1', 'f', 'c', 'x' };
string[] stringArray = new string[] { "array item 1", "array item 2", "array item 3", "array item 4", "array item 5" };

ArraySorter<int>.SortDescending(intArray);
ArraySorter<char>.SortDescending(charArray);
ArraySorter<string>.SortDescending(stringArray);

BindDropDownList(DropDownList1, intArray);
BindDropDownList(DropDownList2, charArray);
BindDropDownList(DropDownList3, stringArray);
}

private static void BindDropDownList(BaseDataBoundControl ddl, Array arr)
{
if(ddl !=null && arr !=null && arr.Length>0)
{
ddl.DataSource = arr;
ddl.DataBind();
}
}
static public class ArraySorter<T> where T : IComparable
{
static public void SortDescending(T[] array)
{
Array.Sort<T>(array, s_Comparer);
}

static private readonly ReverseComparer s_Comparer = new ReverseComparer();

private class ReverseComparer : IComparer<T>
{
public int Compare(T object1, T object2)
{
return -((IComparable)object1).CompareTo(object2);
}
}
}

}
}

I hope it will help you in someway.

Happy Coding!!!

Saturday, December 6, 2008

Highlight Datagrid or GridView row on mouse over in asp.net

To highlight the row and when mouse moves out, the style sheet is switched back to normal.

Use the following two steps to solve this case. This will applicable for both Gridview and Datagrid controls.

Step 1 : Create styles for normal and highlight view of the row.

<style type="text/css">
.normalrow
{
background-color:white;
}
.hightlighrow
{
background-color:#cccccc;
}
</style>
Step 2 : Now add handler for RowCreated event for the grid and add the attributes for onmouseover and onmouseout javascript events.

Following code snippet shows how this has been done.

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.className='hightlighrow'");
e.Row.Attributes.Add("onmouseout", "this.className='normalrow'");
}
}
Use the technology!!!

Friday, December 5, 2008

Allow only numbers / digits in TextBox.

Allow only numbers/digits in TextBox. How to use Javascript to allow only numbers to be entered in a textbox. This is useful for phone numbers, ZipCode, and etc., Use the following two step to solve this situation. This solution works in both IE and Netscape/Mozilla.

Step 1: Add the following javascript into you page.

function AllowNumbersOnly(evt)
{
// Allow numbers only in the Textbox.
var e = event evt; // for trans-browser compatibility
var charCode = e.which e.keyCode;
if (charCode > 31 && (charCode < 48 charCode > 57))
return false;
return true;
}

Step 2: Add the keypress event into your textbox.

<INPUT id="txtChar" onkeypress="return AllowNumbersOnly(event)" type="text" name="txtChar">

In asp.net add your textbox control and use this code behind.

TextBox.Attributes.Add("onkeypress", "javascript:return AllowNumbersOnly(event);");

Use technology!!!