2015-05-08 181 views
1

這是我的頭腦。我嘗試trycatchfinally和其他東西,但此錯誤不斷回來。請有人幫忙。 我的C#代碼:連接未關閉

void show() 
    { 
     string str = "SELECT PID, Pname, Gender, ContactNum, Category, Department, Ward, AdmitDate, DischargeDate, NumOfDays, CostOfTest, NumOfDocsVisited, DocFee, BedCost, TotalCost, GrandTotal FROM Patient"; 
     cmd = new SqlCommand(str, con); 
      con.Open(); 
      SqlDataAdapter adp = new SqlDataAdapter(cmd); 
      DataTable dt = new DataTable(); 
      adp.Fill(dt); 
      GridView1.DataBind(); 
       con.Close(); 
    } 

錯誤是:

The connection was not closed. The connection's current state is open. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The connection was not closed. The connection's current state is open.

源錯誤:

Line 50:   string str = "SELECT PID, Pname, Gender, ContactNum, Category, Department, Ward, AdmitDate, DischargeDate, NumOfDays, CostOfTest, NumOfDocsVisited, DocFee, BedCost, TotalCost, GrandTotal FROM Patient"; 
Line 51:    cmd = new SqlCommand(str, con); 
Line 52:     con.Open(); 
Line 53:     SqlDataAdapter adp = new SqlDataAdapter(cmd); 
Line 54:     DataTable dt = new DataTable(); 

堆棧跟蹤:

[InvalidOperationException: The connection was not closed. The connection's current state is open.] 
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +14 
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +94 
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110 
System.Data.SqlClient.SqlConnection.Open() +96 
SMC.Billing.show() in e:\VS2013 projects\SMC\SMC\Billing.aspx.cs:52 
SMC.Billing.Button3_Click(Object sender, EventArgs e) in e:\VS2013 projects\SMC\SMC\Billing.aspx.cs:97 
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628722 
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 
+0

使用'using'條款,以確保您的連接設置和正確關閉應該清楚這件事。 – asawyer

+0

如何以及在哪裏可以使用? –

+0

在你的代碼中你使用'con'的地方顯然你使用的是全局連接,爲什麼不添加下面的內容或者在調用之前檢查'con'的下面的狀態:'con.Close()'打開方法..但這是一個糟糕的方式..我會做一個條件檢查..並真的很安全..包裝所有的SQL對象圍繞一個'using(){}'這將確保自動處理的對象和基礎對象等... – MethodMan

回答

1

裹在using塊的連接。這保證它總是被關閉。

using (var connection = new SqlConnection(connectionString)) 
{ 
    connection.Open(); 
    //Do stuff with the connection 
} 
2

在我看來,它試圖告訴你,你正試圖打開一個已經打開的連接。

你可以嘗試(在第52行)

if (con.State == ConnectionState.Closed) con.Open; 
+0

我已經試過了 –