28 January 2008

Unsigned drivers in 64 bit Vista (x64)

In 64 bit versions of Windows Vista, you cannot install drivers that are not "signed". This means that if you're trying to install some strange hardware, or some customer drivers, you'll run into problems.

Fortunately you can disable this restriction by running the following from the command prompt...

bcdedit /set loadoptions DDISABLE_INTEGRITY_CHECKS

More details can be found here.

23 January 2008

Slow login system (ASP.Net)

A slower login system is much harder to break into with a brute force attack than a fast one.

If your login system takes 0.1 seconds to say whether the credentials were correct, a hacker could try 36,000 username and password combinations per hour per HTTP connection. If your login system took 5 seconds to return a response, only 720 combinations could be tried per hour per HTTP connection.

In ASP.Net, the following function will make your script pause for 5 seconds...

Threading.Thread.Sleep(5000)

If you're not happy with slowing down your login system for legitimate users, you could modify your script to only pause if the password was incorrect. Just don't tell anyone that's what you're doing!

22 January 2008

<thead> and page breaks

I learned today that printing a table with and tags will cause the header row to be repeated after a page break.

IE7 doesn't do this by default, but with a single CSS definition it can be made to do so...

thead { display: table-header-group; }

16 January 2008

IE caching AJAX requests

I've just discovered the IE caches any AJAX requests made using GET. This means that if you made an AJAX call to the same URL twice, the second request won't get sent, and the response from the first call will be returned instead.

There are two options...

  • Use POST
  • Append a random number to the query string

14 January 2008

MySQL: Sort ascending with blanks at end

It makes sense to have blanks at the start of an A-Z query, as a shorter length string should always appear before a longer length string starting with the same letters. However, sometimes it's convenient to clump any completely black records at the end.

I couldn't think of a perfect way of doing this, but here's a fairly nice solution...

ORDER BY IF(LENGTH(name)>0, CONCAT('1',name), '2') ASC

The IF() function is used to prepend a '1' to any non-zero length string, and '2' to any zero length string. For the latter, we already know that the string is empty so there's no need to use CONCAT().

10 January 2008

Ping a specific port

You cannot ping a specific port, it just doesn't make sense.

However, you can try to establish a Telnet connection to the port. In Windows, go to the command prompt (Start > Run > "command") and type...

telnet example.com 80

Easy when you know how.

If you're using Vista, you'll need to install Telnet first..

Telnet in Vista

Vista does not come with the Telnet client installed by default.

Go to Control Panel and then find the Turn Windows features on or off option. Scroll down to Telnet Client and tick the box. Once the installation is complete, you should be able to use the telnet command from the command prompt.

04 January 2008

DataAdapter and database connections

I didn't know this until a few minutes ago: DataAdaptors manage the opening and closing of the database connection for you.

More specifically, if the connection is not already open, the DataAdaptor will open it and then close it when it is done. If it is already open, the DataAdaptor will leave it open when it's done. Very clever I think.

So, the only time when you'd want to manually open and close the connection is when you want to perform several tasks in a row, all using the same connection.

Raising events from user controls (.ascx)

Just like the DropDownList control has a SelectedIndexChanged event, you can create custom events for your custom user controls.

In the user control...

Public Event MyEvent As System.EventHandler

Protected Sub DoSomething()
    RaiseEvent MyEvent(Me, New EventArgs())
End Sub

And in the main page which contains the user control...

Protected Sub myControl_MyEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles myControl.MyEvent
    'Code goes here
End Sub

Quite good really. Now I just need to figure out how to cause a postback from the user control!

03 January 2008

Casting an array

It looks like there is no built in method for converting an array from one type to another. This DOES NOT work...

Dim arInt As Integer() = {1, 2, 3}
Dim arString As String() = arInt

The only solution seems to be to loop through the original array and insert the elements into a new array one at a time. Seems silly to me.

Don't use DataReader with ObjectDataSource

I develop a lot of database heavy web apps, and sometimes my business layer returns DataReader objects. Like a good little coder I always use CommandBehavior.CloseConnection, like this...

Return ExecuteReader(CommandBehavior.CloseConnection)

I assumed if I passed this to an ObjectDataSource, then .Net would take care of closing the database connection for me, but this is not the case. Even though I've specified the command behavior, it just leaves the connection open.

The best solution seems to be return a DataSet or DataTable, which means that you can manually close the database connection before passing the object to the ObjectDataSource.