views:

354

answers:

2

I have a DAL class library that is included in my program as a DLL. The below line is 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 below error:

An unhandled exception of type 'System.StackOverflowException' occurred in CMO.DAL.dll

The below is in the Settings.Designer.cs file and it is where it shows the error on the get call:

        [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 ideas of what to look for? Is it because the connection string is stored in the dll instead of my Main App? I am really stuck on this and will greatly 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... Any more thoughts? Thanks so far!

EDIT 2

So I followed the suggestion of regenerating the settings file below and now my setting file looks like this -->

public string CMOSQLConn {
        get {
            return ((string)(this["CMOSQLConn"]));
        }
    }

Unfortunately this won't compile now as wherever I have this statement -->

            SqlConnection cnn = new SqlConnection(Settings.CMOSQLConn);

I now get 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!

+7  A: 

This is a classic c# properties mistake. Double check what you're returning in your property-- you're returning the property itself! Name resolution will prefer the local name over an external name. You're getting a stack overflow because you hit infinite recursion when CMOSQLConn.get calls CMOSQLConn.get.

Consider returning Settings.CMOSQLConn. The extra specification should clearly indicate the correct location of your connection string.

EDIT:

Whoops! I didn't notice that you pasted that from your Settings designer file. The infinite recursion is clearly happening, but I'm afraid you'll have to do some more investigation to track down why it's happening in this case.

It appears that your designer file was generated incorrectly (!!!). On VS2008, my settings designer getters look something like:

public bool Foo{
    get {
        return ((bool)(this["Foo"]));
    }
    // ...
}

You may need to do something similar. IE:

public string CMOSQLConn
    get {
        return ((string)(this["CMOSQLConn"]));
    }
    // ...
}
Greg D
Any hints on how to do that? The Call Stack doesn't seem overly useful as it shows what I already posted!
Refracted Paladin
+1  A: 

Try changing your code to this:

 public static string CMOSQLConn {
        get {
            return ((string)(this["CMOSQLConn"]));
        }
    }

Hmm.. Good point in the comments. I just looked in my VS settings file and copied and pasted without thinking. Something isn't right with your settings file... It shouldn't be creating a static property for the settings.

BFree
Can you access this from a static property?
Greg D
It says "Keyword 'this' is not valid in a static property, static method, or static field initializer"Ideas? THanks for the time!
Refracted Paladin