ASP.NET, Technology

How to disable all the ASP.NET input controls within a container control

The following method will recursively disable all the input controls within a container control such as a User Control, PlaceHolder, Panel etc.:

public void DisableInputControls(Control container)
{
	foreach (Control control in container.Controls)
	{
		if (control is TextBox
			|| control is DropDownList
			|| control is RadioButtonList
			|| control is RadioButton
			|| control is CheckBoxList
			|| control is CheckBox
			|| control is Button)
		{
			(control as WebControl).Enabled = false;
		}

		if (control.Controls.Count > 0)
			DisableInputControls(control);
	}
}

Example usage:

DisableInputControls(MyPlaceHolder);
.NET, C#, Technology

How to generate the SHA-1 hash of a string using C#

The namespace System.Security.Cryptography contains classes for working with cryptography such as hashing. Within this namespace you will find the SHA1Managed class which we can utilize to generate the SHA-1 hash of a string. Below you will find a helper method using this class:

public string GetSha1(string value)
{
	var data = Encoding.ASCII.GetBytes(value);
	var hashData = new SHA1Managed().ComputeHash(data);

	var hash = string.Empty;

	foreach (var b in hashData)
		hash += b.ToString("X2");

	return hash;
}
SQL Server, Technology

MS SQL-Server - How to select numeric rows from a varchar column

Luckily T-SQL has an ISNUMERIC(value) function:

select ISNUMERIC('PostalCode123') -- Returns 0
select ISNUMERIC('123') -- Returns 1

Therefore:

select *
from Customer
where ISNUMERIC(PostalCode) = 1

…returns only the rows where the varchar column PostalCode is numeric.

ORM, Technology

Why you should not use the ADO.NET Entity Framework

For some days now I’ve been using the ADO.NET Entity Framework on a pet project of mine. My conclusion so far is that it’s useless in most scenarios, and that I’m lucky to not use it on a real business application. Read More »

Technology, Visual Studio

How to get rid of the alert “The following module was built either with optimizations enabled or without debug information”

If you have a solution in Visual Studio which consists of both web- and class library projects, then you might get an alert box with the following message:

"The following module was built either with optimizations enabled or without debug information:

....

To debug this module, change its project build configuration to Debug mode. To suppress this message, disable the 'Warn if no user code on launch' debugger option."

Do this to get rid of the alert:
1. Right click the project giving the alert, choose “Properties”.
2. Choose “Build” and then “Advanced…”
3. Set Debug info to “full”.

Now the alert should be gone.

AJAX, Technology

Response.Redirect not working within an UpdatePanel?

If you get an exception when doing a Response.Redirect within an UpdatePanel then you are most probably missing the following lines in your web.config:

<httpModules>
  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
JavaScript, Technology

FCKEditor 2.x: How to add a new button with custom javascript to the toolbar

In this tutorial we will have a look at how to add a new button with custom javascript to a FCKeditor toolbar. In this example our custom button will simply open a link in a new window when it’s clicked. Read More »

JavaScript

FCKeditor 2.x: How to get an instance of the editor using javascript

This is how you get an instance of the editor when you are within the page that loads the editor: Read More »

C#, Technology

How to convert from string to enum using C# and generics

In order to convert from a string to an enumeration in .NET you can use the method Enum.Parse as follows:

public enum SortExpressions
{
	FirstNameAsc,
	FirstNameDesc,
	LastNameAsc,
	LastNameDesc
}

var value = "lastnameasc";
var sortExpression = (SortExpressions) Enum.Parse(typeof(SortExpressions), value, true);

Since this operation is often needed we should make a ConvertToEnum method so we can reuse it. But instead of returning it as the typical object type, we should rather use generics to return it as the actual type: Read More »

SQL Server, Technology

Handling comma separated strings in stored procedures

Imagine you have a user interface with a list of products. On the side of each product you have a delete checkbox and then a delete button below the list. Even though a user can delete multiple products at once, you only want to query the database once. One way to achieve this is by sending a comma separated string with product id’s to a stored procedure. The stored procedure will then loop through the string, grab each id and delete the corresponding row in the product table. Read More »

Older Posts »