This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello- I've got an entity named Person that has two properties of interest. Id, where Father is a nullable Backreference to another Person. I use this code to bind a Person instance to a TextBox drawn on a regular Form: Person p = new Person() { FullName = "Whatever" }; Which throws a NullReferenceException and subsequent dialog (see below) when p.Father is null. If p.Father is not null, the binding completes successfully and fully. I don't think the 1-1 nature of Person to 'Father' has any bearing on the problem, rather it seems to be the TextBox and using the Father property. Put another way, binding a ComboBox in this way works fine. this.comboBox1.DataSource = p.Fathers; It is not what I need but it does work fine. This may not be relevant but I did look into it and wrote this paragraph, so... Does anyone have any suggestions on what I might be doing wrong? Alternatively, maybe my approach is entirely messed and I shouldn't be binding in this manner on nullable properties? Any comments or suggestions welcome. I am using Lightspeed version 3.1 on Visual Studio 2010 C#. .NET 4.0. Thank you for your time, Lionel ---------- The Exception: at System.ComponentModel.ReflectPropertyDescriptor.AddValueChanged(Object component, EventHandler handler) My Code: ---------- The Dialog: Unable to add row: |
|
|
This appears to be a Windows Forms issue. I tried using non-LightSpeed objects: public class Alice Then setting up a multi-level binding which went through a null reference like this: Alice a = new Alice { Name = "alice" }; // leaving Bob as null I didn't get the exception message or dialog, but all the data bindings on the form (including the textBox1 binding) failed. So it looks like Windows Forms doesn't handle the case of a binding trying to traverse through a null reference. The different handling of the error may be a LightSpeed issue, but even if this is the case and even if we were to fix it, it appears you would still be left with Windows Forms trashing all the other bindings on the form. So I think you will need to use a different technique, or ensure that Father is never null. That said, I'm not terribly familiar with WinForms data binding, and haven't been able to find any documentation on null reference behaviour or whether it can be customised, so I may be missing something... |
|
|
Hello Ivan- Thank you for your time. I'll investigate other techniques. As an aside, you wrote "I'm not terribly familiar with WinForms data binding". I've always found Winforms data binding to be cumbersome and problematic. Maybe it's just me. Nonetheless, I welcome any recommendations for alternative (Better?) techniques to exchange data between Mindscape and my UI. The Mindscape Winforms examples use Winforms data binding so I just kind of stayed with it when I moved to Mindscape. Thank you again! I appreciate your time very much. Lionel |
|
|
I think the samples use data binding because we had a lot of queries about using data grids, which are obviously very binding-oriented. And of course binding is a convenient technique for many scenarios. However, when you hit the limits of binding, as you've done with your nullable traversal case, you need to fall back on ye olde code. There are a number of patterns for this, of which probably the most popular in the WinForms environment is Model-View-Presenter (MVP). (An alternative which might help you keep your binding infrastructure, and is more idiomatic if you plan to move to WPF, is Presentation Model aka Model-View-ViewModel -- essentially layering an adapter over your real data model to make it more binding-friendly.) I'm pretty sure there are a bunch of MSDN and other articles about this -- a bit of googleage should see you right. This article covers some of the LightSpeed specific considerations: http://www.mindscape.co.nz/blog/index.php/2009/08/19/tips-for-rich-client-development/ Hope this helps! |
|
|
Hi Lionel, You shouldn't get an error dialog. I've attached a solution have a look at it and let me know if that is what you wanted to do.
Kind regards, Johan |
|
|
Thank you Ivan - I appreciate your comments and suggestions and time. Thank you Jonah. I appreciate your time as well however I have been unable to download your attachment - the link throws me to a 404. I'll check back on the link over the coming days and from a different workstation to see if I can retrieve it later. Regardless, I greatly appreciate the time you spent putting it together. Thank you. -Lionel |
|
|
Hi Lionel, I've attached a zip file please check if you can download this one? Johan |
|
|
Hi Johan- I was able to get the ZIP but the 7ZIP extension remains unavailable here. Thank you! I spent some time looking into why your version works and mine doesn't. Simply put, I see that using your explicit BindingSource rather than relying on the one built on-the-fly by .NET works around the aforementioned exception. What you've demonstrated is exactly what I wanted to do so I'll add those constructions into this particular Form. Thank you again! Once this project is done, I'll visit Ivan's suggestions as well. Thank you for the direction and the help - I appreciate your time! Best regards, Lionel |
|
|
It's only a pleasure to help. All the best, Johan
|
|