1

I am getting a null reference exception when trying to connect to the database via ConnectionCtrings["MyDB"].connectionstring.

This is in a using statement and is the same code I've used in many other projects, but it keeps telling me null reference and I don't know why.

The connection string named is named correctly in the web.config which is in the same project so I wouldn't expect there to be permissions issues.

What have I missed?

Edit: I have seen the suggested answers, these are solved by putting the string in the Web.Config which is where the connection string is.

Code: ConnectionString in Web.config

<connectionStrings>
    <add name="MyDB" connectionString="Data Source=192....; Initial Catalog=ProjectDb; Integrated Security=false; User Id=user; Password=password;" providerName="System.Data.SqlClient" />
</connectionStrings>

Function to access DB

public static Company RetrieveCompany(int id)
{
    var cmp = new Company();
    try
    {
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
        {
            con.Open();
            using (var cmd = new SqlCommand("RetreiveEmailProc", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@companyId", id);

                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                { // code omitted }
            }           

        }
    }
    catch(Exception ex)
    {

    }

    return cmp;
}
Daniel
  • 9,491
  • 12
  • 50
  • 66
PurpleSmurph
  • 2,055
  • 3
  • 32
  • 52

1 Answers1

1

I think it is related to you using ConfigurationManager instead of the WebConfigurationManager. It is possible your code resides in another folder with a different web.config and the ConfigurationManager can't handle that inheritance problem.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks for the suggestion, unfortunately it gave the same error using WebConfigurationManager. – PurpleSmurph Nov 10 '16 at 10:21
  • Can you set a break point and see what is in `ConfigurationManager.ConnectionStrings`? – Patrick Hofman Nov 10 '16 at 10:27
  • I have done - what am I looking for? When I hover over WebConfigurationManager I get count 2, which I would think is wrong, surely there should only be one? – PurpleSmurph Nov 10 '16 at 10:29
  • Can you see the items in there? Is your connection in there? I guess the default connections from your `machine.config` are in there too. – Patrick Hofman Nov 10 '16 at 10:31
  • Strange, it doesn't appear to have anything related to the connection string it's pointed at, which would explain the null exception, but not sure as to what's causing it. – PurpleSmurph Nov 10 '16 at 10:35
  • @PurpleSmurph 2 connection strings is correct. Your connection string and one called "LocalSqlServer" which is some built in connection to a SQL Express database. – Colin Mackay Nov 10 '16 at 10:35
  • What are the names of the connection strings? – Patrick Hofman Nov 10 '16 at 10:37
  • Colin Mackay is correct, there are two, one pointing to local and one pointing to {}, which I guess is where the problem lies. – PurpleSmurph Nov 10 '16 at 10:39
  • Something strange is happening here. There is no connection with the name you expect? Does one of the connections have the connection string you expect? – Patrick Hofman Nov 10 '16 at 10:40
  • Yeah it's rather odd. One connection string is Local (as Colin rightly pointed out) and the other is blank (showing as {}) these can both be seen when I hover over ConnectionStrings and go down to ResultsView [0] is local and [1] is the blank string. – PurpleSmurph Nov 10 '16 at 10:43
  • There must be something wrong with the name. Can you paste that connection string in a UTF-8 capable editor (like Notepad++) and see if there are weird characters in it? – Patrick Hofman Nov 10 '16 at 10:44
  • There doesn't seem to be, on further inspection when I switched back to ConfigurationManager, the count is still 2 but the second result, whilst blank is essence has the name MySqlServer (I'm not using MySQL) – PurpleSmurph Nov 10 '16 at 10:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127792/discussion-between-patrick-hofman-and-purplesmurph). – Patrick Hofman Nov 10 '16 at 10:49