Tuesday, February 27, 2007

InitilizeComponent() visited

Well these are two scenarios that happened to me:
In one of the earlier projects, just before a release I was working on a alignment issue in an aspx page and no change to the code behind file was done. But when the setup was tested on a fresh machine, the application threw exceptions !!. Finally what we found out was that the code behind of the file has really changed, to make things worse, the URL of the web service was hardcode to “Localhost” . After the release (went home at 6.30 in the morning that day), had some time to investigate this matter.
What I found was that someone has instantiated the web service proxy reference inside the InitilizeComponent() , and every time there is a change in the aspx page design , the InitilizeComponent() gets regenerated, and because the web service proxy reference was instantiated inside this method , the auto-generated code assigns the URL property of the proxy to “Localhost”.

I ran into the same sort of problem yesterday, the application was running fine. But once I changed the language property of the form and ran, a NullReferenceException was thrown. Again some one has instantiated a custom resource manager inside the InitilizeComponent(), and when the language is changed for the form , this method is regenerated , and the user written code is lost!
Thus, a NullRefernceException

So, don’t every try to change the InitilizeComponent() method, you may never know where the problem may come from.
If you need custom code that needs to be written when initializing components, do it in the constructor, just after the Initilize component method.


This problem was in VS2003, but in VS2005 the auto generated designer code goes into a seperate partial class.

No comments:

Post a Comment