0

Im writing a class project in C# and I am having an issue with the connection string in the app.config file. (SQL Server Database)

using a unit test my connection string does not work in the following code

`

public DataSet GetDataSet(string sqlString)
            {
            if(sqlConn.State == ConnectionState.Open)
                { sqlConn.Close(); }
            string connectionString = ConfigurationManager.AppSettings["strSql"];
            //string connectionString = "Server =.; Database = TNAME; Trusted_Connection = True; ";
            sqlConn = new SqlConnection(connectionString);
           
            sqlConn.Open();
            //
            sqlCmd.Connection = sqlConn;

`

However if i swap the string as follows then it works what am i missing

`

  public DataSet GetDataSet(string sqlString)
            {
            if(sqlConn.State == ConnectionState.Open)
                { sqlConn.Close(); }
           // string connectionString = ConfigurationManager.AppSettings["strSql"];
            string connectionString = "Server =.; Database = TNAME; Trusted_Connection = True; ";
            sqlConn = new SqlConnection(connectionString);
           
            sqlConn.Open();
            //
            sqlCmd.Connection = sqlConn;

`

app.config file code

`

<connectionStrings>
    <add name="strSql" connectionString="Server =.; Database = TNAME; Trusted_Connection = True;"/>
  </connectionStrings>
  <appSettings>
    <!--   User application and configured property settings go here.-->
    <!--   Example: <add key="settingName" value="settingValue"/> -->
        <add key="strSql" value="Server =.; Database = TNAME; Trusted_Connection = True; "/>
   </appSettings>

`

Error Message System.InvalidOperationException: 'The ConnectionString property has not been initialized.'

Mote: I also tried using

`

public DataSet GetDataSet(string sqlString)
            {
            if(sqlConn.State == ConnectionState.Open)
                { sqlConn.Close(); }
            //string connectionString = ConfigurationManager.AppSettings["strSql"];
            string connectionString = ConfigurationManager.ConnectionStrings["strSql"].ConnectionString;
            //string connectionString = "Server =.; Database = TNAME; Trusted_Connection = True; ";
            sqlConn = new SqlConnection(connectionString);
           
            sqlConn.Open();
            //
            sqlCmd.Connection = sqlConn;

`

I tried using the app.config using the appsettings and connectionstring properties see inline code above.

zwheeler
  • 1
  • 1
  • See the this [thread](https://stackoverflow.com/a/22785729/5509738) regarding reading another apps configuration file. If using .NET Core consider using appsettings.json, there are plenty examples on the web. – Karen Payne Nov 03 '22 at 22:09

1 Answers1

0

Try to create app.config file like this -

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="strSql" value="Server =.; Database = TNAME; Trusted_Connection = True;" />
    </appSettings>
</configuration>

You read the above application settings using the code shown below:

using System.Configuration;


string configvalue1 = ConfigurationManager.AppSettings["strSql"];

You may also need to also add a reference to System.Configuration in your project if there isn't one already.

  • Added System.Configuration - copied exactly what you have for App.config and still getting ConnectionString Property has not been initialized on sqlConn.Open(); – zwheeler Nov 07 '22 at 17:14
  • I have created a simple example project for you. [link](https://github.com/Konohamaru04/ConsoleAppTest)`Konohamaru04/ConsoleAppTest`. Please check how it is implemented in this. Test is the main project and DbClassLib is a class library project. – Amartya Deshmukh Nov 11 '22 at 07:27