"system.invalidoperationexception was unhandled" on
<p>I receive 'InvalidOperationException was unhandled'. con.Open(); Is then highlighted.</p>

<p>My app is working fine if i use connection string in the .cs file but i get the exception if i place it in web.config. Tried everything available on google but no luck.</p>

<p><strong>Code</strong></p>

<pre><code>using System.Configuration;
using System.Web.Configuration;

public DataSet GetInvoice()
{
      SqlConnection con = new SqlConnection();
     con.ConnectionString =            WebConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; 

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select * from invoice", con);
            **con.Open();**
            da.Fill(ds);
            con.Close();
            return ds;
        }
</code></pre>

<p><strong>Web.config</strong></p>

<pre><code>&lt;connectionStrings&gt;       
    &lt;add name ="MyConnection" connectionString = "Data Source =.\\SQLEXPRESS; AttachDbFilename = C:\\USERS\\SALIL\\DOCUMENTS\\INVOICING.MDF ; Integrated Security = True" providerName="System.Data.SqlClient" /&gt;
  &lt;/connectionStrings&gt; 
</code></pre>

<p><strong>StackTrack :</strong>   </p>

<pre><code> at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at WebInvoicing.Invoice.GetInvoice() in C:\WebInvoicing\WebInvoicing\Invoice.cs:line 220
   at WebInvoicing.InvoiceApp.LoadGrid() in C:\WebInvoicing\WebInvoicing\InvoiceApp.aspx.cs:line 124
   at WebInvoicing.InvoiceApp.Page_Load(Object sender, EventArgs e) in C:\WebInvoicing\WebInvoicing\InvoiceApp.aspx.cs:line 13
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
</code></pre>
