views:

104

answers:

3

Hello,

I have created a custom data grid control. I dragged it on windows form and set its properties like column and all & ran the project. It built successfully and I am able to view the grid control on the form.

Now if i try to view that form in designer, I am getting following error..

Object reference not set to an instance of an object.     




Instances of this error (1)  

1.   Hide Call Stack 

at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.GetMemberTargetObject(XmlElementData xmlElementData, String& member)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.CreateAssignStatement(XmlElementData xmlElement)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.XmlElementData.get_CodeDomElement()
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.EndElement(String prefix, String name, String urn)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.Parse(XmlReader reader)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.ParseXml(String xmlStream, CodeStatementCollection statementCollection, String fileName, String methodName)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
at System.CodeDom.CodeMemberMethod.get_Statements()
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload) 

If I ignore the exception, form appears blank with no sign of grid control on it. However I can see the code for the grid in the designer file.

Any pointer on this would be a great help.

I have customized grid for my custom requirements like I have added custom text box n all. I have defined 3 constructors

public GridControl() 
public GridControl(IContainer container) 
protected GridControl(SerializationInfo info, StreamingContext context)
A: 

It sounds like a NullReferenceException is thrown in your control's default constructor (the one without parameters). Obviously, this exception is only thrown at design time since you say it works at runtime. Do you perform any initialization code in this constructor, like data base calls or similar? Or do you use any instances which might not be available at design time?

gehho
I tried using try..catch block in all the constructors. But I am not able to catch any exception.I am not doing any database call in initialization. I am just creating columns and all.
Ram
Try removing all the code in your default constructor. Does the problem still occur? If it does not, re-add your constructor code line by line (or block by block), and you will see which part of your code causes the problem. Remember to recompile your project after every change in the code because otherwise the designer will use the code from the last compile.
gehho
I do not believe this has anything to do with constructors. Notice that Serialization is throwing the exception. Specifically, GetMemberTargetObject. I believe this is due to a missing member on the control. The designer is trying to restore a property's value and the property is missing.
AMissico
A: 

It looks like the form-designer is trying to initialize the control. Yet, the property it is trying to initialize may have been removed from the UserControl. There are lots of way to troubleshoot this problem. I recommend you debug the control in design-time. It is the surest way to find the problem. Check out "MSDN Search" for "design-time control debugging" at http://social.msdn.microsoft.com/Search/en-US?query=design-time+control+debugging&ac=8

AMissico
A: 

I have this problem all the time...it sucks.

[Ramble(on)]

Here is what I think I know:

  1. When designing place the control on a form. Build and refresh often..this will let you know what change caused the designer to barf.
  2. Close visual studio all the way an re-open....I cannot tell you how many times I have chased a designer error that was the designer being "stuck".
  3. It is very important that you understand : The designer is really, really stupid...like bag of rocks stupid.
  4. Any public fields or properties of custom object types will almost always cause designer confusion*. I find the following attributes will clear up most of these problems:

    [Browsable(false)]

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

  5. Public fields or properties without a default constructor will always cause designer confusion. When you drop a user control on a form the designer effectively creates the control..so any public object needs a clear creation path. I have found that the easiest way around this (read hack) is keeping the non-trivial custom classes private and expose public properties as a facade.

-- Did I say restart visual studio because sometimes the designer is "stuck" on an error that doesn't exist ?..I hope I did.

[Ramble(off)]

I hope some of this helps..

*designer confusion: Instead of showing your controls the designer shows you a useless error message that might or might not include the dire warning that it is protecting you from code loss...blah, blah.

Rusty