PC Review


Reply
Thread Tools Rate Thread

Binding TextBox.Text to property in nested (multi levels) object withbindingsource, Error if grandchild object is NULL

 
 
Efy
Guest
Posts: n/a
 
      14th Oct 2009
Hi all,



I am binding a TextBox to an object which has child object which has
a grandchild object, and I want to bind to the property of the
grandchild object.

I want to show/update on the form/UserControl the nested property.

i.e. "Child.GrandChild.Description" is the DataMember of the
BindingSource.

But, if the GrandChild object is null, I get on error when disposing
the form:

"Value cannot be null. Parameter name: component" (I added in the
bottom the stack trace)

To fix this, we can set the DataSource of the bindingSource to null
before the form is disposed, but the problem is that in my project
which is too big to bring up (It’s build with CAB and pretty tied to
the DB), I get the same error when a UserControl finishes to load not
only when it's disposed, but I just manage to reproduce the error when
the form disposes and I can't realize why.

So I think that if I find a proper workaround for this, I will be able
to fix my error too.

Any Ideas?



The following is the code to reproduce the Error, all you need is a
binding source object that the DataSource will be set to typeof
(Parent) before you set the data member:

this.textBox1.DataBindings.Add("Text", this.class1BindingSource,
"Child.GrandChild.Description", true);





namespace NullDataBindingTest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}





private void Form1_Load(object sender, EventArgs e)

{

//this.descriptionTextBox.DataBindings.Add("Text",

this.class1BindingSource, "Child.DescObject.Description",

true,DataSourceUpdateMode.OnPropertyChanged,"NONE");

this.textBox1.DataBindings.Add("Text",

this.class1BindingSource, "Child.GrandChild.Description", true,

DataSourceUpdateMode.OnPropertyChanged, "NONE");

Parent c1 = new Parent();

c1.Child = new Child();

//c1.Child.GrandChild = new GrandChild();

this.class1BindingSource.DataSource = c1;

}

}





public class Parent

{

private string _name;





public string Name

{

get { return _name; }

set { _name = value; }

}





private Child _child;





public Child Child

{

get { return _child; }

set { _child = value; }

}





}





public class Child

{

private string _name;





public string Name

{

get { return _name; }

set { _name = value; }

}





private GrandChild _grandChild;





public GrandChild GrandChild

{

get { return _grandChild; }

set { _grandChild = value; }

}





}





public class GrandChild

{

private string _description;





public string Description

{

get { return _description; }

set { _description = value; }

}





public override string ToString()

{

return Description;

}

}







}



I also added my StackTrace.

StackTrace: at

System.ComponentModel.ReflectPropertyDescriptor.RemoveValueChanged

(Object component, EventHandler handler)

at System.Windows.Forms.BindToObject.CheckBinding()

at System.Windows.Forms.Binding.CheckBinding()

at System.Windows.Forms.Binding.SetBindableComponent

(IBindableComponent value)

at System.Windows.Forms.ControlBindingsCollection.ClearCore()

at System.Windows.Forms.BindingsCollection.Clear()

at System.Windows.Forms.ControlBindingsCollection.Clear()

at System.Windows.Forms.Control.ResetBindings()

at System.Windows.Forms.Control.Dispose(Boolean disposing)

at System.Windows.Forms.TextBox.Dispose(Boolean disposing)

at System.ComponentModel.Component.Dispose()

at System.Windows.Forms.Control.Dispose(Boolean disposing)

at System.Windows.Forms.ContainerControl.Dispose(Boolean
disposing)

at System.Windows.Forms.Form.Dispose(Boolean disposing)

at NullDataBindingTest.Form1.Dispose(Boolean disposing) in C:
\Users

\Public\!SVN\SandBox\NullDataBindingTest\NullDataBindingTest

\Form1.Designer.cs:line 20

at System.ComponentModel.Component.Dispose()

at System.Windows.Forms.Form.WmClose(Message& m)

at System.Windows.Forms.Form.WndProc(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.OnMessage

(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc

(Message& m)

at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr

hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr

wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)

at System.Windows.Forms.Form.DefWndProc(Message& m)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.ScrollableControl.WndProc(Message& m)

at System.Windows.Forms.ContainerControl.WndProc(Message& m)

at System.Windows.Forms.Form.WmSysCommand(Message& m)

at System.Windows.Forms.Form.WndProc(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.OnMessage

(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc

(Message& m)

at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr

hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr

wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)

at System.Windows.Forms.Form.DefWndProc(Message& m)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.ScrollableControl.WndProc(Message& m)

at System.Windows.Forms.ContainerControl.WndProc(Message& m)

at System.Windows.Forms.Form.WmNcButtonDown(Message& m)

at System.Windows.Forms.Form.WndProc(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.OnMessage

(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc

(Message& m)

at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr

hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&

msg)

at

System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsa*
feNativeMethods.IMsoComponentManager.FPushMessageLoop

(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

at

System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner

(Int32 reason, ApplicationContext context)

at System.Windows.Forms.Application.ThreadContext.RunMessageLoop

(Int32 reason, ApplicationContext context)

at System.Windows.Forms.Application.Run(Form mainForm)

at NullDataBindingTest.Program.Main() in C:\Users\Public\!SVN

\SandBox\NullDataBindingTest\NullDataBindingTest\Program.cs:line 18

at System.AppDomain._nExecuteAssembly(Assembly assembly, String[]

args)

at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence

assemblySecurity, String[] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly

()

at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

at System.Threading.ExecutionContext.Run(ExecutionContext

executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding TextBox to Nested/Complex property - and what happens if theChild object is null? EfyT Microsoft C# .NET 1 15th Oct 2009 05:36 PM
Binding text box to object's property not properly bound! teddysnips@hotmail.com Microsoft C# .NET 0 15th May 2007 04:29 PM
Binding data to object and its nested object (ASP.NET 2.0) Piotrek Microsoft ASP .NET 4 4th Aug 2006 11:09 AM
Property of object to binding Boniek Microsoft C# .NET 2 6th Apr 2004 05:01 PM
Binding a Business object to a datagrid with multiple levels Liam Ponder Microsoft ADO .NET 1 11th Aug 2003 10:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:56 AM.