This code compiles/runs but breaks designer...

C

Charlie@CBFC

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
 
B

Bryan Phillips

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;
}
}
 
C

Charlie@CBFC

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".



Bryan Phillips said:
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



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
 
B

Bryan Phillips

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



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".



Bryan Phillips said:
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



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
 
C

Charlie@CBFC

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?




Bryan Phillips said:
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



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".



message
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




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
 
B

Bryan Phillips

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



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?




Bryan Phillips said:
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



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".



message
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




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
 
C

Charlie@CBFC

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!







Bryan Phillips said:
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



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?




message
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




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".



message
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




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
 
G

Guest

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;
}
}
 
C

Charlie@CBFC

That's what I ended up doing. Thanks.
Kaustav said:
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;
}
}


Charlie@CBFC said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top