Databinding with Nested Properties

B

Brian Pelton

I am trying to bind a textbox to the property of a property.
This code works when nested property is not null,
but it fails when nested is null.

(It fails when SiteLine.Site is null.)

The error is ArgumentNullException: component.


I setup the binding in the constructor of my Form.

Code:
txtAcres.DataBindings.Add("Text", _chemicalRecsDBS,
"SiteLine.Site.CropAcres", true);
txtAcres.DataBindings["Text"].Format += new
ConvertEventHandler(Lib.AppCommon.DataBinding.IntFormatHandler);
txtAcres.DataBindings["Text"].Parse += new
ConvertEventHandler(Lib.AppCommon.DataBinding.IntParseHandler);
txtAcres.DataBindings["Text"].NullValue = null;

_chemicalRecsDBS is bound to an IList<ChemicalRec>.

Here are the relevant class definitions:

Code:
Class ChemicalRec
{
public ChemicalRecSite SiteLine
{
get { ... }
set { ... }
}
}

Class ChemicalRecSite
{
public SiteInfo Site
{
get { ... }
set { ... }
}
}

Class SiteInfo
{
public int? CropAcres
{
get { ... }
set { ... }
}
}
 
L

Linda Liu [MSFT]

Hi Brian,

I performed a test based on your code and did reproduce the problem.

When we bind a control to a property of data source, the value of the bound
property in the data source could be null, e.g. in your practice, the value
of the SiteLine.Site.CropAcres could be null and there will be no
exception.

However, if the SiteLine.Site property of an instance in the data source is
null, data binding couldn't find the Site property of the SiteLine property
of the instance, so exception occurs. This behavior is by design.

A workaround to this problem is to ensure that SiteLine.Site propery is not
null.

Hope this helps.
If you have any questions, please feel free to let us know.

BTW, I will be on a long vacation from the next Monday to Friday. During my
leave, my team mates will follow up with you and it may not in time. Sorry
for the inconvenience it may bring to you!



Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brian Pelton

Linda said:
I performed a test based on your code and did reproduce the problem.

I had to smile when you said that. In my dealings with vendors
(PeopleSoft, Oracle, etc), usually they cannot reproduce the problem.
However, if the SiteLine.Site property of an instance in the data source is
null, data binding couldn't find the Site property of the SiteLine property
of the instance, so exception occurs. This behavior is by design.

Darn. I was hoping I was just missing a step somewhere. It's so handy
to just type the full property path.

A workaround to this problem is to ensure that SiteLine.Site propery is not
null.

In my example, the user hasn't picked a Site yet, so it's null. I guess
I will have to hook up to an event when the Site property is populated,
and at that time, make my databindings.

I was hoping to set all the databindings in the form constructor, but as
you say it isn't possible.

Thanks for looking at this,
Brian

BTW, I will be on a long vacation from the next Monday to Friday. During my
leave, my team mates will follow up with you and it may not in time. Sorry
for the inconvenience it may bring to you!

Have a great vacation!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top