{"id":3014,"date":"2010-05-06T14:53:50","date_gmt":"2010-05-06T14:53:50","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2010\/05\/06\/another-error-creating-control-in-the-design-view-with-object-reference-not-set-in-visual-studio-2010\/"},"modified":"2010-05-06T14:53:50","modified_gmt":"2010-05-06T14:53:50","slug":"another-error-creating-control-in-the-design-view-with-object-reference-not-set-in-visual-studio-2010","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/another-error-creating-control-in-the-design-view-with-object-reference-not-set-in-visual-studio-2010\/","title":{"rendered":"Another Error Creating Control in the Design View with Object Reference Not Set in Visual Studio 2010"},"content":{"rendered":"<p>In a previous blog, <a href=\"http:\/\/blogs.msdn.com\/webdevtools\/archive\/2010\/04\/15\/rendering-issue-in-visual-studio-2010-when-accessing-the-session-state-in-the-oninit-method.aspx\" target=\"_blank\" rel=\"noopener\">Controls State: &quot;Error Creating Control&quot; in the Design View in Visual Studio 2010<\/a>, I have discussed about an Error Creating Control issue related to accessing the Session state in the OnInit() method. Since then, I have discovered that the Error Creating Control will also show in the design view if we try to access any object not being instantiated at the design time inside the OnInit() method.<\/p>\n<p>For example, in a web application project, in a code-behind file that is adding controls dynamically to a page, similar to what was discussed in the article <a href=\"http:\/\/www.asp.net\/ajax\/videos\/how-to-dynamically-add-controls-to-a-web-page\" target=\"_blank\" rel=\"noopener\">How To Dynamically Add Controls to a Web Page<\/a> , we have code that\u2019s accessing an object that is null at the design time as shown below<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/OnInit.png\"><img decoding=\"async\" style=\"border-right-width: 0px;margin: 0px 0px 0px 20px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"OnInit\" border=\"0\" alt=\"OnInit\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/05\/OnInit_thumb.png\" width=\"531\" height=\"282\" \/><\/a> <\/p>\n<p>When you switch to the design view, you would see &quot;<font color=\"#ff0000\"><strong>Error Creating Control<\/strong>&#160;<\/font>\u2013 xxx. Object reference not set to an instance of an object\u201d, where xxx is a control on your page. Screen shot of the error is shown below<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/Error.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"Error\" border=\"0\" alt=\"Error\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/05\/Error_thumb.png\" width=\"362\" height=\"341\" \/><\/a> <\/p>\n<p>A simple workaround for this issue would be to check if we are in the design mode at the top of the OnInit() method, below the base.OnInit() call.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/FixOnInit.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"FixOnInit\" border=\"0\" alt=\"FixOnInit\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/05\/FixOnInit_thumb-1.png\" width=\"644\" height=\"364\" \/><\/a> <\/p>\n<\/p>\n<p>This is a bug in VS 2010 that we\u2019re now calling the OnInit() method at the design time; we are considering a fix for this in a future release of Visual Studio.<\/p>\n<p>Thank you,    <br \/>-Anh     <br \/>Visual Web Developer Team<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous blog, Controls State: &quot;Error Creating Control&quot; in the Design View in Visual Studio 2010, I have discussed about an Error Creating Control issue related to accessing the Session state in the OnInit() method. Since then, I have discovered that the Error Creating Control will also show in the design view if we [&hellip;]<\/p>\n","protected":false},"author":404,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[7359,7342,7323,7363],"class_list":["post-3014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-controls","tag-designer","tag-visual-studio-2010","tag-vs2010"],"acf":[],"blog_post_summary":"<p>In a previous blog, Controls State: &quot;Error Creating Control&quot; in the Design View in Visual Studio 2010, I have discussed about an Error Creating Control issue related to accessing the Session state in the OnInit() method. Since then, I have discovered that the Error Creating Control will also show in the design view if we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/404"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=3014"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3014\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=3014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=3014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=3014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}