PC Review


Reply
Thread Tools Rate Thread

This code compiles/runs but breaks designer...

 
 
Charlie@CBFC
Guest
Posts: n/a
 
      14th Apr 2007
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




 
Reply With Quote
 
 
 
 
Bryan Phillips
Guest
Posts: n/a
 
      14th Apr 2007
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



"Charlie@CBFC" <(E-Mail Removed)> wrote in message
news:OYB$(E-Mail Removed):

> 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


 
Reply With Quote
 
Charlie@CBFC
Guest
Posts: n/a
 
      14th Apr 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
> "Charlie@CBFC" <(E-Mail Removed)> wrote in message
> news:OYB$(E-Mail Removed):
>
>> 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

>



 
Reply With Quote
 
Bryan Phillips
Guest
Posts: n/a
 
      14th Apr 2007
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



"Charlie@CBFC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed):

> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >
> >
> > "Charlie@CBFC" <(E-Mail Removed)> wrote in message
> > news:OYB$(E-Mail Removed):
> >
> >> 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

> >


 
Reply With Quote
 
Charlie@CBFC
Guest
Posts: n/a
 
      15th Apr 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
> "Charlie@CBFC" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed):
>
>> 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" <(E-Mail Removed)> wrote in
>> message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> >
>> >
>> > "Charlie@CBFC" <(E-Mail Removed)> wrote in message
>> > news:OYB$(E-Mail Removed):
>> >
>> >> 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
>> >

>



 
Reply With Quote
 
Bryan Phillips
Guest
Posts: n/a
 
      15th Apr 2007
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



"Charlie@CBFC" <(E-Mail Removed)> wrote in message
news:#n$(E-Mail Removed):

> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >
> >
> > "Charlie@CBFC" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed):
> >
> >> 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" <(E-Mail Removed)> wrote in
> >> message
> >> news:(E-Mail Removed)...
> >> > 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
> >> >
> >> >
> >> >
> >> > "Charlie@CBFC" <(E-Mail Removed)> wrote in message
> >> > news:OYB$(E-Mail Removed):
> >> >
> >> >> 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
> >> >

> >


 
Reply With Quote
 
Charlie@CBFC
Guest
Posts: n/a
 
      16th Apr 2007
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>
>
> "Charlie@CBFC" <(E-Mail Removed)> wrote in message
> news:#n$(E-Mail Removed):
>
>> 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" <(E-Mail Removed)> wrote in
>> message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> >
>> >
>> > "Charlie@CBFC" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed):
>> >
>> >> 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" <(E-Mail Removed)> wrote in
>> >> message
>> >> news:(E-Mail Removed)...
>> >> > 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
>> >> >
>> >> >
>> >> >
>> >> > "Charlie@CBFC" <(E-Mail Removed)> wrote in message
>> >> > news:OYB$(E-Mail Removed):
>> >> >
>> >> >> 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
>> >> >
>> >

>



 
Reply With Quote
 
=?Utf-8?B?S2F1c3Rhdg==?=
Guest
Posts: n/a
 
      18th Apr 2007
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" wrote:

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

 
Reply With Quote
 
Charlie@CBFC
Guest
Posts: n/a
 
      27th Apr 2007
That's what I ended up doing. Thanks.
"Kaustav" <(E-Mail Removed)> wrote in message
news:0B861FF8-C49F-4AB4-93A0-(E-Mail Removed)...
> 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" wrote:
>
>> 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
>>
>>
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code that compiles under both ASP.NET 1.1 and 2.0? wizofaus@hotmail.com Microsoft ASP .NET 4 6th Feb 2007 02:17 PM
BUG: Designer breaks UI code Microsoft C# .NET 6 11th Dec 2003 06:28 PM
BUG: Designer breaks UI code Microsoft Dot NET 4 11th Dec 2003 06:28 PM
BUG: Designer breaks UI code Microsoft Dot NET Framework Forms 4 11th Dec 2003 06:28 PM
code compiles c++ 6.0, not in c++ .NET Steve Richter Microsoft VC .NET 14 29th Oct 2003 01:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:17 PM.