Tuesday, July 27, 2010

Web User Control: Cannot create an object of type 'System.Int32' from its string representation

I had an interesting bug today. I have been creating web user controls based on the structure of database table fields. When I dropped the web user control on to a web form and tried to access its properties from the codebehind, I was getting no intellisense. I then looked at the designer.aspx.cs and notice the web user control was not registering.

After some digging I found an article that seemed to describe my issue that allowed me to narrow down the problem. I followed the "Simple Tip" of deleting the designer.ascx.cs and then regenerating it and found that it was failing because I was using a public property called "Id". Visual Studio 2008 IDE complains about this, and rightfully so. In hindsight I can see why I shouldn't use a reserved name like "Id" for custom a property.

I changed the property name and all references, and I was back in business. It would have been nice to have a better error message to narrow this down quickly, but I'm grateful to have found the issue quickly.