Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Differentiating between NULL and Empty String in POST (See related posts)

// We were having a problem where it was not possible to detect the difference between a value being passed with no value and the value not being passed at all. This was a problem because we have a singe update page to handle most of the database saves; it would loop through all the possible values of a data object setting them based on what was passed in the form. If a column was not present in the original form it would be set to NULL by the update page. The work around was to place these values in the original form as hidden elements; this meant that whenever we added a column it had to be added to the form(s). This was obviously not a tenable solution so I came up with the following method of requesting a value from a POST or GET.

#region Private Properties

string[] _formKeys;
string[] _queryStringKeys;

#endregion 

#region Web Page Properties

/// <summary> A list of keys found in Request.Form </summary>
protected string[] FormKeys
{
	get
	{
		if (this._formKeys == null)
			this._formKeys = Request.Form.AllKeys;
		return this._formKeys;
	}
}

/// <summary> A list of keys found in Request.QueryString </summary>
protected string[] QueryStringKeys
{
	get
	{
		if (this._queryStringKeys == null)
			this._queryStringKeys = Request.QueryString.AllKeys;
		return this._queryStringKeys;
	}
}
#endregion

#region public methods

///<summary>Grabs parameter from Form or Query String. Never returns NULL.</summary>
/// <param name="paramName">The name of the parameter to be pulled</param>
/// <returns>Value of parameter. Returns String.Empty if no value or parameter found.</returns>
protected string requestParam(string paramName)
{
	string result = requestParam(paramName, false);
	return (result == null) ? String.Empty : result.Trim();
}

///<summary>Grabs parameter from Form or Query String. </summary>
/// <param name="paramName">The name of the parameter to be pulled</param>
/// <param name="returnNullIfParamNotFound">Will return NULL if parameter was not passed in the form or query string. If false an empty string will be returned.</param>
/// <returns>Value of parameter. Returns String.Empty parameter found but has no value.</returns>
/// <returns></returns>
protected string requestParam(string paramName, bool returnNullIfParamNotFound)
{
	string result = String.Empty;
	if (Context.Request.Form.Count != 0)
	{
		result = Convert.ToString(Context.Request.Form[paramName]);
	}
	if (Context.Request.QueryString.Count != 0 && (result == String.Empty || result == null))
	{
		result = Convert.ToString(Context.Request.QueryString[paramName]);
	}
	if (result == null && returnNullIfParamNotFound)
	{
		// check the form keys
		if (stringFoundInArray(this.FormKeys, paramName))
			return string.Empty; //doing a return to skip the rest
		// check the query string keys
		if (stringFoundInArray(this.QueryStringKeys, paramName))
			return string.Empty; 
	}
	return result;

}

/// <summary>
/// Performs a case insensitive search of an string array for a value
/// </summary>
/// <param name="array"></param>
/// <param name="value"></param>
/// <returns>Boolean indicating if value was found or not</returns>
public static bool stringFoundInArray(string[] array, string value)
{
	for (int x = 0; x < array.Length; x++)
		if (string.Equals(value, array[x], StringComparison.OrdinalIgnoreCase))
			return true;
	return false;
}



#endregion

You need to create an account or log in to post comments to this site.


Click here to browse all 7716 code snippets

Related Posts