Archive for the ‘Uncategorized’ Category

Jquery Rich Array Documentation

April 30th, 2010 by Sameer | No Comments | Filed in Uncategorized

If you want to use the JQuery Rich Array and you were hoping for some documentation, well its inside the .JS file but I am posting it here for reference purposes

/***************************************************************************
 *   Copyright (C) 2007 by Vladimir Kadalashvili                                        *
 *   Vladimir.Kadalashvili@gmail.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/*
A simple jQuery plugin for varios manipulations with arrays
*/ 

jQuery.richArray = {

    /*
        Checks whether an array contains some value
	@param {Array} array - an array in which we search for a value
	@param {Mixed} value - the value we search for
	@return {boolean} true if the array contains the value, otherwise false
    */

    in: function(array, value) {
    },

    /*
        Produces the duplicate-free version of the array
	@param {Array} array
	@returns {array}  - an array without duplicates
    */
    unique: function(array) {

    },

    /*
        Finds the difference between two arrays.
	@param {Array} array1
	@param {Array} array2
	@return {Array} array of values which are present in the first array, but not in the second
    */

    diff: function(array1, array2) {

    },

    /*
        Finds the intersection of two arrays
	@param {Array} array1
	@param {Array} array2
	@return {Array} - the array of values wich are present in both arrays
    */

    intersect: function(array1, array2) {

    },

    /*
        Applies filter to the array, using callback function
	@param {Array} array - an array which we apply filter to
	@param {Function} fn - the filter function. If it returns the value that may be evaluated as TRUE, the value will be included to the returned array.
	@param {Object} scope - the scope of the callback function. Default is jQuery.richArray
	@returns {Array} - an array of values for which callback function returned true
    */

    filter: function(array, fn, scope) {

    },

    /*
        Applies callback function for each element in the input array, and returns array of values that this function returned
	@param {Array} array - an array which we should apply callback to
	@param {Function} fn - callback function
	@param scope - the scope of the callback function. Default is jQuery.richArray
    */
    map: function(array, fn, scope) {
    },

    /*
        Computes the sum of all array elements.
	@param {Array} array - an array we should compute the sum for
	@param {Mixed} init - the initial value of the sum. Default is 0.
	@returns {Mixed} the sum of all elements of the input array
    */

    sum: function(array, init) {
    },

    /*
        Calculates the production of all elements of the array
	@param array - an array we should compute production for
	@param init - the initial value. Default is 1.
	@returns {Mixed} - the production of all elements of the input array
    */

    product: function(array, init) {
    },

    /*
        Reduces the array. One-elemen arrays are turned into their unique element, others are retured untouched
	Examples:
	jQuery.richArray.reduce([3]) -> 3
	jQuery.richArray.reduce([3, 5]) -> [3, 5]
    */

    reduce: function(array) {
    },

    /*
        Creates new version of array without null/undefined values
	@param {Array} array - input array
	@returns {Array} - an array without null/undefined values
    */

    compact: function(array) {
    },

    /*
       Creates a new version of the array that doesn't contain the specified value
       @patam {Array} array - input array
       @param {Mixed} value - the value that shouldn't be included to the returned array
       @returns {Array} - a new version of the input array without specified value
    */

    without: function(array, value) {
    },

    /*
        If the passed argument is an array, returns it untouched, otherwise returns an empty array.
	For internal use.
    */

    getArray: function(array) {
    },

    /*
        if the passed argument is a function, returns it untouched, otherwise returns an empty function
   */

    getFunction: function(fn) {
        if (!(fn instanceof Function)) fn = new Function();
	return fn;
    }    

};

Homepage: Jquery Rich Array Plugin

How to completely disable ViewState and ControlState

April 21st, 2010 by Sameer | No Comments | Filed in Uncategorized

Here is a code snippet that will COMPLETELY disable ViewState and ControlState.

Please note, if you want to disable viewstate, you can set “EnableViewState” to false for the page, however you will still see “VIEWSTATE” in the page. The reason for that is because the hidden ViewState HTML field also contains “Control State”, which is used by server controls (either built in controls or custom 3rd party controls) and is not disable-able (for core functionality that their control depends on).

However you can still disable this, with disastrous consequences on postback.

USE THIS only if you have NO POSTBACKS whatsoever and you are living in a happy client side world of javascript and web service calls.

Put this following code inside your ASP.NET page

private class DummyPageStatePersister  :P ageStatePersister
{
    public DummyPageStatePersister(Page p) : base(p) { }
    public override void Load() { }
    public override void Save() { }
}

private DummyPageStatePersister _PageStatePersister;
protected override PageStatePersister PageStatePersister
{
    get
    {
        if (_PageStatePersister == null)
            _PageStatePersister = new DummyPageStatePersister(this);
        return _PageStatePersister;
    }
}

Please note this will has disastrous consequences if you attempt to do a postback because you have essentially killed the control state.

The Most Useful .NET Utility Classes Developers Tend To Reinvent Rather Than Reuse

August 7th, 2009 by Sameer | No Comments | Filed in Uncategorized

I think this is a classic post that everyone should read:

The Most Useful .NET Utility Classes Developers Tend To Reinvent Rather Than Reuse

Annoying Nulls in SQLParameters

August 7th, 2009 by Sameer | 4 Comments | Filed in Uncategorized

If you read Creating SqlParameters Best Practices you will find the fun you have if you have null values:

SqlParameter[] sqlParams = new SqlParameter[] {
  new SqlParameter("@Required", required),
  questionCode == null ? new SqlParameter("@Code", DBNull.Value) : new SqlParameter("@Code", questionCode)
};

Here is a nice helper function to deal with nulls without having to manually check every time.

        /// <summary>
        /// Return a SqlParameter with DBNull value or value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static SqlParameter NullWrapper(string key, object value)
        {
            if (value == null)
                return new SqlParameter(key, DBNull.Value);
            else
                return new SqlParameter(key, value);
        }

Then you can use it as follows

            SqlParameter[] sqlParams = new SqlParameter[] {
                new SqlParameter("@UserID", userId),
                new SqlParameter("@itemNo", itemNo),
                General.NullWrapper("@expiryDate", expiryDate) //no need to check if null any more
            };

ASP.NET 4.0 Features

May 27th, 2009 by Sameer | No Comments | Filed in Uncategorized

Here is a list of new features in ASP.NET 4.0.

Very interesting. Auto start feature, new ways to choose where data is cached, built in option for compression session (via gzip), and more!

Write .NET 3.5 in a .NET 2.0 World

April 2nd, 2009 by Sameer | No Comments | Filed in Uncategorized

Wow, sweet. Use .NET 3.5 features in .NET 2.0.

A quick overview how to do it.
1. Edit your .CSPROJ file and manually add another reference to System.Core
Add a True

This will make the compiler copy it to the BIN folder.

Thats it. You can now use .NET 3.5 features

Copy Data from One SQL Instance to Another

February 3rd, 2009 by Sameer | 2 Comments | Filed in Uncategorized

When using SQL Server Management Studio you might want to copy data from one DB to another (using just a SELECT and INSERT statement)

It’s so easy you won’t believe it

sp_addlinkedserver @server='192.168.123.456', @provider='SQLNCLI',    @srvproduct='',@provstr='User Id=sa; Password=are-you-crazy-to-use-sa'

insert into [192.168.123.456].MYDB.dbo.tblRecords

select * from MYDB.dbo.tblRecords

and when you are done

sp_dropserver  [192.168.123.456]

References:
sp_addlinkedserver on MSDN

sp_dropserver on MSDN

What is hnc.cgi ?

December 13th, 2008 by Sameer | No Comments | Filed in Uncategorized

hnc.cgi is a spam script. Its also known as dm.cgi.

If you have this script running, chances are your server has been exploited.

Here is the actual dm.cgi script if you want to see it.

Changes in __doPostback in .NET 1.1 to .NET 2.0

November 27th, 2008 by Sameer | No Comments | Filed in Uncategorized

Before and After

If you manually want to simulate a postback, Here is how you would stick together a string manually calling __doPostBack in .NET 1.1
In this case its making a link in a particular linkbutton control inside a datagrid.

__doPostBack in .NET 1.0

string link = "<a href=\"javascript:__doPostBack('dataGridCart$_ctl" + i + "$linkButton','');\"";

__doPostBack in .NET 2.0

string link = "<a href=\"javascript:__doPostBack('dataGridCart$ctl0" + i + "$linkButton','');\"";

Dont use this function any more.  Use Page.GetPostBackClientHyperlink from .NET 2.0+.  This is because they might change __doPostBack yet again and your code will be broken.

Look at the comment thread on this codeproject article for more details

Further reading: Do Postback Hijacking

Tracking Influenza (flu) with Google Trends

November 13th, 2008 by Sameer | No Comments | Filed in Uncategorized

This is quite an astonishing use of Google Trends.  Google realized that when people get sick they (obviously) search for flu related keywords and they managed to find a correlation to actual published flu infection statistics.  Quite amazing and this is definately a unique and interesting application of technology.

How we track flu trends (Google blog)
Here is an explanation of how it works

What other ideas can we apply this to?