SharePoint 2010: Visual Webpart tutorial
One of the scourges of software development, and it seems especially SharePoint development, is that more often than not, when faced with a new task, Google is your first stop, looking for tutorials or guides. Frequently, however, people posting the tutorials (MSDN/Microsoft included) are guilty of putting up guides and source code that is incorrect, or missing some vital info. I experienced this very issue recently when I needed to create a visual webpart.
The tutorial is good enough, but the actual source code posted, doesn’t work. What’s most infuriating, though, is when the author then goes radio silence, and the comments fill up with bemused readers, all looking to achieve the same goal - but unable to do so.
There is only one offending file in the solution: MyWebPart.cs. I have amended the broken variable names, and included some missing code for actually adding the drop down menu to the Webpart. Note: you will need to update the site location in your version, and you should be able to derive this automatically (i.e., not have it fixed.), and also the name of your custom list.
Enjoy. (And, of course, if I’ve got the code wrong, let me know ;))