14

I'm pretty sure there's some quick and easy error in this code but somehow I've spent the last 2 hours with this and couldn't solve it.

App.config:

<configuration>
  <connectionStrings>
    <add name="BO"
        connectionString="Data Source=MyServer;Initial Catalog=BO;User ID=WebUser;Password=MyPasswd"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Class.cs:

string connectionString = getNewConnection();
using (SqlConnection conn = new SqlConnection(connectionString)) { code }

Method.

public static string getNewConnection()
{
   return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
 }

Error:

Object reference not set to an instance of an object

on the line :

return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;

EDIT:

Error image, the Spanish sentence means: Object reference not set to an instance of an object

4 Answers 4

10

It should be:

ConfigurationManager.ConnectionStrings["BO"].ConnectionString;

Edit:

You will need the corresponding libraries as well if you don't have them yet, as mentioned in the below answers I think its System.Configuration

So in full you should have:

public static string getNewConnection()
{
    return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
}
Sign up to request clarification or add additional context in comments.

11 Comments

Corrected the library, it is System.Configuration
if I use "ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings["BO"].ConnectionString;" I get the error "Cannot implicitly convert type 'string' to 'System.Configuration.ConnectionStringSettingsCollection'"
You don't need to do that, in your getNewConnection() function simply have return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
the same error pops up in that line. Weird, would my app.config be wrong?
it's solved! the app.config was in the same project as the class.cs, but for testing purposes I had created a new one with a windows form containing a datagrid with a simple select * from table from the desired server. I had to put the app.config inside that one project. (the reference was made to the Class.cs project so I didn't think that could matter at all) but indeed it did matter... I knew it was something that silly. Thanks a lot for your patience and help.
|
2

Use these codes in the Class :

class Connection
    {
        public static string con
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            }
        }
    }

1 Comment

it gives me the same error (now referencing to your line of code). Would my app.config be wrong?
1

Did you use the WebConfigurationManager?

string MyConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BO"].ConnectionString;

1 Comment

it says System.Web does not containg .Configuration etc. (I have added using System.Web )
0

Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.