Thursday, May 22, 2008

Unable To Cast Object of Type 'x' To Type 'x'

I got this really weird runtime error:

Unable To Cast Object of Type 'x' To Type 'x'

The weird thing was, the 'x' above was the same type.

The best explanation I found to this issue is:
http://www.hanselman.com/blog/FusionLoaderContextsUnableToCastObjectOfTypeWhateverToTypeWhatever.aspx

Basically, I had 2 identical dll files that contain the type 'x' in different paths on the server. One of the dlls was referenced in my project, while the other was loaded using the LoadFrom() function from the System.Reflection.Assembly namespace.
When it was time to cast an object to type 'x', I got the error "Unable To Cast Object of Type 'x' to Type 'x' ".

To resolve this problem, I simply deleted the extra dll reference. This will ensure the same assembly will be used in the project reference and when LoadFrom() is used.

Tuesday, May 20, 2008

readOnly textboxes

Sometimes you want to set a textbox to read only because you don't want the user to change the value inside. This is the case when the value in the textbox is, for example, the date the user has selected from a calendar control, or the result of some calculation.

To make the textbox read only, you would normally set the ReadOnly attribute to True or enabled = false in the .aspx page.

However, if you want to process this value on the server (e.g. save it to a database), the value is not recognised. This is because the server does not process read only values.

To fix this, set the following in the code behind, for example:

txt.Attributes.Add("readOnly", "true")

Wednesday, May 14, 2008

SQL Transaction Logs

Today I was getting the following error while deleting records from a very large table (over 7.6 million rows):

The log file for database 'xxxxx' is full. Back up the transaction log for the database to free up some log space. [SQLSTATE 42000] (Error 9002). The step failed.

Deleting large amounts of data will cause this type of error to occur since deleted rows are entered into the transaction log. Truncating the table would overcome this problem, however truncating would delete all the records which I didn't want as I had to keep the last 2 years worth of data.

So I tried to do what the error message said, and that was to backup the transaction log:

BACKUP LOG XXXXX with truncate_only

and I also shrunk the log file for good measure:

dbcc shrinkfile ('XXXXX_Log', 'truncateonly')

After running these 2 lines and confirming that the size of the transaction log was reduced (20MB down to less than 1MB), I tried a second time to delete the table, and again it gave me the same error!

After some time of investigating and searching the net for more answers, I realised that the problem was a lack of hard drive space. The available space was only less than 100MB which wasn't enough to carry out the delete command on my large table.

The solution was to move the database to another hard drive with plenty of free space on it. This was easily done using the Detach and Attach commands in SQL Enterprise manager. This can only be done after hours when there are no connections to the database.

Thursday, May 8, 2008

The CreateObject of '(null)' caused exception C0000005.

I had to upgrade an old-school ASP website the other day. I uploaded the new version of the .asp pages, unregistered the website .dll file, made a backup copy of it, copied the new .dll over, and then registered it. Everything when to plan (so I thought) until it was time to test the site. I then received the following error:

The CreateObject of '(null)' caused exception C0000005.

I thought to myself - how could this happen? I've tested the .dll file on a test site and had no problem with it. So I immediately when to the source code and had a look at the line where it was giving the error. Funny that, the line giving the error was not modified by me! In fact the page was not modified at all.

So I decided to restore the original version of the .dll (good thing I did make a backup), and it too gave me the same error. Very strange.

The only thing left to do was an IIS restart. And that fixed the problem.

So the problem was probably due to IIS still having a reference to the old version of the .dll, and restarting IIS was a way of flushing it out.

Sunday, May 4, 2008

ASPX Page Events

I thought I would investigate the firing order of .aspx page events as my first post (well, second post actually as the first one was just a quick intro).

For this test, I created a new web project inVS2008 and simply added a server button on the aspx form, with a bunch of Response.writes in each event I found in the code behind, except inside the unload events, as a Response.write in the unload event will cause a runtime error to occur.

The result of this quick test is as follows:
  • New
  • Page PreInit
  • button Init
  • form Init
  • page Init
  • Page InitComplete
  • Load Viewstate *
  • Page Preload
  • Page Load
  • form Load
  • button Load
  • Button Click ^
  • Button Command ^
  • Page LoadComplete
  • Page PreRender
  • form PreRender
  • button PreRender
  • page PreRenderComplete
  • page SaveStateComplete
  • button Unload
  • form Unload
  • page Unload
* To determine when viewstate is loaded, I placed the following code inside the form load event:
If Not Page.IsPostBack Then
Me.ViewState("x") = "x"
End If

I then monitored when the variable x in the view state changed from nothing to x using Watch after clicking on the button.

^ these events were fired after a page postback.

As you can see, the viewstate is loaded after the page init event, but before the page load event. This is very important to remember if you are planning to work with viewstates in aspx.

Also, note that postback events (eg. button Click), occur after page Load, but before the page LoadComplete events.

Welcome to my blog!

Hi, I would like to invite you to my blog. Here I will try and share my experiences as a Microsoft .Net developer.