Wednesday, December 21, 2005

VS2005 Design-time set DataSource error III

Design-time data-binding in .NET 2.0 WinForms is a real productivity booster that I use extensively. I have, however, had some problems with using the new object binding source mechanism (error I, error II) and today yet another problem. I began designing a new user-control a few days ago, and being a lazy developer, I copied some data bound comboboxes that I needed from another user-control.

I should have know better, copying controls was a recipy for immediate disaster in the VS2005 betas; you could be sure that the Visual Studio designer would fail when you later on re-opened the user-control. The same applied to renaming a control after modifying some of its properties. These problems have been fixed in the release version of VS, although opening a form still randomly causes the cursor to go into a blink frenzy for quite some time while re-syncing with the .Designer file.

Copying those combos kept some of the data binding properties (ValueMember, DisplayMember), but did not copy the binding sources (more on this below). I added the object binding sources in the 'Data Sources' explorer, and after some refreshing of the project data sources (error II), object binding seemed to work properly.

Today, I added another object binding source to the 'Data Sources' explorer of the user-control. If I then tried to use the DataSource property at design-time, I got this error:

Object reference not set to an instance of an object

Setting the data binding at run-time work fine. So does copying the original data source binding code from the hidden .Designer file, it even shows correctly in the control properties explorer, but cannot be changed due to the above error. But this is not how I want to do it, I wanted design-time binding to work. Any attempt to modify data binding on any controls in the form would cause the design-time error.

In an attempt to fix the binding problems, I used an old trick when working with software and computers; I restarted Visual Studio, opened the solution and cleaned it. Then I clicked the DataSource property of my failing combo. Now I got this error, which anyone who have parsed XML will recognize:

Root element is missing

After Visual Studio had shown this puzzling error (as usual no intelligble info provided in the message), all design-time data binding worked properly again and the combos were filled at run-time. Oh joy! . . . But the object binding source that I had added lastly, was gone from the 'Data Sources' explorer. As long as the design-time binding kind of worked again, I could live with that.

I suspected that there was a problem with one XML files in the solution and checked the various .RESX files and some other files with XML content, but I was not been able to find any error. When I had just about given up, I noticed that the three listed data sources in the 'Data Sources' explorer also happened to be the three first .datasource files in the \My Project\DataSources\ folder (use 'Show all files' in the 'Solution Explorer'). Thus, I opened the fourth .datasource file, and voila, it was empty! Visual Studio clearly just stops looping through the data sources when it encounters an error.

I excluded the empty file from the project, reloaded the solution, and then all the project data sources were listed in the 'Data Sources' explorer. Design-time binding now works as expected again. Oh, even more joy!

I am still sceptical to copying object bound controls from one WinForms user-control to another. The VS designer might punish you. Maybe I am just being paraniod.

12 comments:

Anonymous said...

Thanks for the tip -- I had the same problem with the DataSource wizard not coming up and turns out that there was a DataSource file in that folder that was pointing to a class that I had deleted.

Anonymous said...

Thanks - I had a similar problem. I renamed a type I had created and this lead to the design time errors you described. Even though the dataSource has been autmatically converted to
the new name.

Brian Shapiro said...

your a life saver. i never would have thought about that but had the same exact problem for days- that you just helped me resolve in seconds. thanks again.

Anonymous said...

thanks again... I was on the verge of putting an axe through my screen...

Anonymous said...

Thanks for your valuable tip.. it resolved my issues in sec's...i had same problem for days..

Anonymous said...

Thanks a MILLION!!!

You saved my life...

Ubercoder

Trhacz said...

I had similiar problem. I had to remove all my project datasources, modify project file (clean up datasource section), remove reference to my busines object assembly. After that I put binding source on form and wizard started work normally. Thanks for you help.

Anonymous said...

Thanks a lot!, very good tip

Guy Soffer said...

Thanks! it helped a lot.

Juan said...

Thanks for the tip! I was become crazy for that for

Anonymous said...

Thanks for your help. I would not have been able to sort this out on my owm.

Anonymous said...

Thanks alot!