Settings.Designer File and Static
I have a DAL class library that is included in my program as a DLL. The next line is output from the DAL to initialize the connection.
DataSet ds = new DataSet("table");
SqlConnection cnn = new SqlConnection(Settings.CMOSQLConn);
When I run this I get the following error:
An unhandled exception of type 'System.StackOverflowException' occurred in CMO.DAL.dll
Below is the Settings.Designer.cs file where it shows an error when calling get:
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=WWCSTAGE;Initial Catalog=CMO;Persist Security Info=True;User ID=CMOWe" +
"bService;Password=ecivreSbeWOMC")]
public static string CMOSQLConn {
get {
return (CMOSQLConn);
}
}
Anyone have any idea what to look for? Is it because the connection string is stored in the dll instead of my main application? I am really stuck on this and would really appreciate any help!
EDIT 1
I tried Greg's suggestion below:
public static string CMOSQLConn {
get {
return (Settings.CMOSQLConn);
}
}
And I still get the same error ... More thoughts? Thanks so far!
EDIT 2
So, I followed the suggestion to restore the settings file below and now my settings file looks like this:>
public string CMOSQLConn {
get {
return ((string)(this["CMOSQLConn"]));
}
}
Unfortunately this won't compile now like wherever I have this statement ->
SqlConnection cnn = new SqlConnection(Settings.CMOSQLConn);
Now I am getting this error ->
Error 1 An object reference is required for the non-static field, method, or property 'CMO.DAL.Properties.Settings.CMOSQLConn.get' B:\MyDocs\tmpPATRIOT\Projects\VS2008\DisConnectDAL\CMO.DAL\SupportWorker.cs 13 51 CMO.DAL
Is this what I should expect?
Thanks!
a source to share
This is a classic C # property error. Double check what you are returning your property - you are returning the property itself! Name resolution prefers local name over external name. You get a stack overflow because you are pushing infinite recursion when CMOSQLConn.get
calling CMOSQLConn.get
.
Consider a refund Settings.CMOSQLConn
. The additional spec should clearly state the correct location for your connection string.
EDIT:
Oops! I didn't notice that you were pasting this from the settings designer file. Infinite recursion is certainly happening, but I'm afraid you'll have to do some more research to figure out why this is happening in this case.
It looks like your constructor file was not created correctly (!!!). On VS2008, my designer getters settings look something like this:
public bool Foo{
get {
return ((bool)(this["Foo"]));
}
// ...
}
You may need to do something like this. IE:
public string CMOSQLConn
get {
return ((string)(this["CMOSQLConn"]));
}
// ...
}
a source to share
Try changing your code to this:
public static string CMOSQLConn {
get {
return ((string)(this["CMOSQLConn"]));
}
}
Hmm .. Nice point in the comments. I just looked into my VS settings file and copied and pasted without a second thought. Something is wrong with your settings file ... It shouldn't create a static property for settings.
a source to share