1

This is a noob question but I have been stuck here for a few hours so I need to get passed this. I have this Windows App that performs a simple function. It makes a trip to the DB and checks if a specific record exists and if it does then perform some operation.
However it just doesn't want to read the connection string - it comes as null all the time.
I keep getting null every time I initialize my connection string. What could I be doing wrong. I only have one connection string named App.config.

Class File:

 private class ClassA 
 {
     private string myConnectionString = "";
     private SqlConnection mySQLConnection;
     private SqlCommand mySQLCommand;

     private int CheckIfSerialNumberExists(UInt64 ColumnToCheck)
     {
         int countResult = 0;
         myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; //I get an object reference null here when the compiler executes this line. I have been using this structure for years and never got any issues
         using (mySQLConnection = new SqlConnection(myConnectionString))
         {
              string procName = "SELECT Count(*) ColumnName FROM Table WHERE ColumnName='" + ColumnToCheck + "'";
              mySQLCommand = new SqlCommand(procName, mySQLConnection);
              mySQLConnection.Open();
              countResult = (int)mySQLCommand.ExecuteScalar();
         }
         return countResult;
     }

     private void someFunc()
     {
        //Test value: 5
        if(CheckIfSerialNumberExists(5) > 0)
        {
           //Don't do anything
        }
        else
        {
           //Save to DB
        }
     }
 }

Config File:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>   
    <add name="ConnectionStringName"
         connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=**;Password=****"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup>
</configuration> 
Harold_Finch
  • 682
  • 2
  • 12
  • 33
  • Possible duplicate of [Get connection string from App.config](https://stackoverflow.com/questions/6536715/get-connection-string-from-app-config) – ProgrammingLlama Dec 13 '17 at 11:28
  • I do see a problem in your code but it's not related to the question. I can't find the reason for your nullReferenceException. However, you are using SqlConnection and SqlCommand as class level members, and they should be local variables inside your methods. Other then that your code seems fine to me. – Zohar Peled Dec 13 '17 at 11:29
  • Is your config file copied to the output folder? – Peter Szekeli Dec 13 '17 at 11:30

1 Answers1

4

So here is the quick fix:

In my windows app I am referencing a class library project. I had only added the connection string in that project. I added it to my windows app project and there we go, all was working. Really feel like an idiot.

Harold_Finch
  • 682
  • 2
  • 12
  • 33
  • 2
    Well, first I'm glad you found the solution to your problem. Second, It's good you posted it as an answer. It might help future readers. Third, I'll bet that's a lesson you will not forget, and fourth, something similar happened to me and also to a lot of programmers I know. Since I refuse to believe we are all idiots, I guess you are also not an idiot. – Zohar Peled Dec 13 '17 at 14:22