all groups > dotnet windows forms > april 2007 >
You're in the

dotnet windows forms

group:

This code compiles/runs but breaks designer...


This code compiles/runs but breaks designer... Charlie NO[at]SPAM CBFC
4/14/2007 11:03:32 AM
dotnet windows forms:
Hi:

This code compiles and runs fine, but I get this error when try to open a
form in WinForm designer. Commenting it out fixes problem. Weird!

"Object reference not set to an instance of an object.
at RepairShop.DataService.get_ConnectionString() in C:\My
Projects\RepairShop\DataService.cs:line 16"


public static string _connectionString = String.Empty;
public static string ConnectionString {
get {
if( _connectionString == String.Empty ) {
_connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
return _connectionString;
}
}

Thanks!
Charlie



Re: This code compiles/runs but breaks designer... Charlie NO[at]SPAM CBFC
4/14/2007 4:05:20 PM
Code compiles and runs so it's getting connection string. Problem is this
line of code...
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()
breaks designer. Says "Object reference not set to an instance of an
object".



[quoted text, click to view]

Re: This code compiles/runs but breaks designer... Charlie NO[at]SPAM CBFC
4/14/2007 7:18:37 PM
I don't understand Bryan. Are you saying that code runs when I open Form
for editing? So conditions might not be right at design time, but are right
at runtime? How do you troubleshoot something like this? Doesn't make
sense to me to have to kludge it just so it works at design time.

I get error "Object reference not set to an instance of an object" when I
open form, so it must be null. Not sure how to test it without running
program and stepping through code?




[quoted text, click to view]

Re: This code compiles/runs but breaks designer... Bryan Phillips
4/14/2007 7:31:35 PM
Change your code to test if
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"]
is null before calling ToString().

Ex:

public static string _connectionString = String.Empty;
public static string ConnectionString {
get {
if( _connectionString == String.Empty ) {
if
(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"]
!= null) {
_connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
}
return _connectionString;
}
}

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net



[quoted text, click to view]
Re: This code compiles/runs but breaks designer... Bryan Phillips
4/14/2007 10:21:52 PM
Did you try my suggestion? Code also runs at design-time too.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net



[quoted text, click to view]
Re: This code compiles/runs but breaks designer... Bryan Phillips
4/15/2007 1:25:59 PM
Yes, that is what I am saying. Visual Studio will run the constructor
and all properties for any component that you open in design mode.

If you need to step through the code, modify your project
properties->debugging tab to start a new instance of Visual Studio.

Once the new instance of Visual Studio is open, open the project again
and then open the form. You can set a breakpoint in the form's property
in the first instance of Visual Studio so you can step through the code.



--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net



[quoted text, click to view]
Re: This code compiles/runs but breaks designer... Charlie NO[at]SPAM CBFC
4/16/2007 7:50:12 AM
Thanks Bryan, I got it. There are lists that are databinding in a
constructor and need a connection string. So I changed code to check if
ConfigurationManager.ConnectionStrings["ConnectionString"] == null at design
time, and if so, set it to connection string on development box. At
runtime, it correctly looks to app.config. So I guess bottom line is
app.config can't be a accessed at design time. Worthy lesson!







[quoted text, click to view]

RE: This code compiles/runs but breaks designer... Kaustav
4/18/2007 4:44:04 AM
You can also use -

public static string ConnectionString {
get {
if (this.Site !=null && this.Site.DesignMode)
{
_connectionString = "Dev box Connection String";
}
else if( _connectionString == String.Empty ) {
_connectionString =

System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
return _connectionString;
}
}


[quoted text, click to view]
Re: This code compiles/runs but breaks designer... Charlie NO[at]SPAM CBFC
4/27/2007 6:54:45 PM
That's what I ended up doing. Thanks.
[quoted text, click to view]

AddThis Social Bookmark Button