“system.invalidoperationexception未处理”

时间:2012-09-23 22:26:55

标签: c#

我收到'InvalidOperationException未处理'。 con.Open();然后突出显示。

如果我在.cs文件中使用连接字符串,我的应用程序工作正常,但如果我将它放在web.config中,我会得到异常。尝试谷歌上的一切,但没有运气。

代码

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)

1 个答案:

答案 0 :(得分:3)

无需转义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> 

有关详细信息,请参阅Handle backslash in the connection string