Ani
Ani

Reputation: 11

"system.invalidoperationexception was unhandled" on

I receive 'InvalidOperationException was unhandled'. con.Open(); Is then highlighted.

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.

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;
        }

Web.config

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

StackTrack :

 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)

Upvotes: 1

Views: 1956

Answers (1)

akton
akton

Reputation: 14376

There is no need to escape the slashes in the connection string in the web.config file. It should be:

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

See Handle backslash in the connection string for more information.

Upvotes: 3

Related Questions