Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

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

Reply
 
Old 10-14-2009   #1 (permalink)
Efy


 
 

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

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()

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Object Binding Query .NET General
Microsoft VBScript runtime error: Object doesn't support this property or method: 'WScript.Sleep' VB Script
capture the error message as a property object PowerShell
Re: "Error: 'null' is null or not an object" when trying to view video Vista performance & maintenance
Adding canonical aliases for Compare-Object, Measure-Object, New-Object PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46