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> 
3
  • Possible duplicate of Get connection string from App.config Commented Dec 13, 2017 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. Commented Dec 13, 2017 at 11:29
  • Is your config file copied to the output folder? Commented Dec 13, 2017 at 11:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.