Subscribe to
Posts
Comments

Archive for the 'Code Sample' Category

Having upgraded the website I work on to .net 2.0, one of our console apps failed whilst talking to a webservice on the site. We’d not changed anything on either the console app, or the website other than upgrading the website to 2.0.
The error message was “global:System.Collections.IList cannot be deserialized because it does not have [...]

We’ve got a moderately large database here - 80gb, 5 tables and 500m rows - and we had problems updating it. Following a bout of Horizontal Partitioning we’ve now got 130 tables and those rows distributed evenly across them. However when a check constraint was updated today SQL Server 2000 wouldn’t allow data to import [...]

Spent a while trying to get sub-projects opening within Visual Studio today where the root project has SSL security setup. Every time VS tried to open the sub-projects it would error with the message “Secure channel failed”.
Figured it out eventually - you need to add a location element to the web.config for the get_aspx_ver.aspx to [...]

[csharp]
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace SocialAnimal.Web.UserControls
{
/// <summary>
/// If ControlToValidate has a value then DependantControl
/// must also have a value
/// </summary>
public class DependantFieldValidator : CustomValidator
{
private string _dependantControl = null;
private bool _showAlertBox = false;
[Description(@"If true, will show an alert box if the control validates false, otherwise will display an inline message"),
Category("Behavior")]
public bool ShowAlertBox
{
get { return _showAlertBox; [...]

Here’s a quick example of how we do layer seperation through interfaces to make each layer testable. Although the tests run, the UI section is untested. Download file.
[csharp]
using System;
using System.Web;
using System.Web.UI;
using NMock;
using NUnit.Framework;
using Socialanimal.Example.Business;
using Socialanimal.Example.Business.Implementation;
using Socialanimal.Example.Core.Entities;
using Socialanimal.Example.Core.Interfaces;
using Socialanimal.Example.SqlDatabase;
namespace Socialanimal.Example.Core.Entities
{
public class Employee
{
public string Forename;
public string Surname;
public Employee(string forename, string surname)
{
this.Forename = forename;
this.Surname = surname;
}
}
}
namespace Socialanimal.Example.Core.Interfaces
{
public interface [...]

I’ve just been reading up on mocks for testing and they look like a pretty neat and powerful way to improve your testing. I’ve written this sample code based on the example Martin Fowler gives in his excellent article.
This example is of an order which attempts to fullfil itself from a Warehouse. Here’s the Warehouse [...]

Normally if you were trying to sort a DataTable you would do see something like
[csharp]
DataTable table = retrieveTable();
DataView view = new DataView(table);
view.Sort = “Name”;
datagrid1.DataSource = view;
datagrid1.DataBind();
[/csharp]
Doing this works fine for UI cases, but if you need to get access to the sorted table you’ll want to do this.
[csharp]
DataTable unsortedTable = retrieveTable();
DataTable sortedTable = new DataTable();
foreach [...]

Taken from this blog is a technique which will postback the form when you hit enter. If you use the script below, it will work in any browser (although bypassing any onClick code for the button specified).
[javascript]
function clickButton(e, buttonid)
{
var bt = document.getElementById(buttonid);
if (typeof bt == ‘object’)
{
try
{
if(navigator.appName.indexOf(”Netscape”)>(-1))
{
if (e.keyCode == 13)
{
bt.click();
return false;
}
}
if (navigator.appName.indexOf(”Microsoft Internet Explorer”)>(-1))
{
if (event.keyCode == [...]

[csharp]
private void dgNumbers_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList drpList = (DropDownList)e.Item.FindControl(”drpStyleEdit”);
drpList.Items.FindByText(((BE.ProgrammeStyle)e.Item.DataItem).Description).Selected = true;
}
}
[/csharp]

To reduce page bloat, we move view state to session state, rather than store it in hidden fields. This is done by overriding System.Web.UI.Page’s LoadPageStateFromPersistenceMedium() and SavePageStateToPersistenceMedium() methods.
[csharp]
protected override object LoadPageStateFromPersistenceMedium()
{
return Session["__ViewState"];
}
protected override void SavePageStateToPersistenceMedium(object state)
{
Session["__ViewState"] = state;
}
[/csharp]

Next »