2015-04-02 62 views
-1

我得到這個錯誤超時已過期。在從池中獲取連接之前已超時。

超時已過期。在從池中獲取連接之前已超時。發生這種情況的原因可能是因爲所有連接池都在使用中,並且達到最大池大小。

我看了this question,它聲明異常是因爲沒有關閉連接而引起的。然而,我關在我的代碼的所有連接

這是我的代碼,它是簡單

public partial class index : System.Web.UI.Page 
    { 

     private static string defaultReason = "reason not selected"; 
     protected override object SaveViewState() 
     { 
      //save view state right after the dynamic controlss added 
      var viewState = new object[1]; 
      viewState[0] = base.SaveViewState(); 
      return viewState; 
     } 

     protected override void LoadViewState(object savedState) 
     { 
      //load data frm saved viewstate 
      if (savedState is object[] && ((object[])savedState).Length == 1) 
      { 
       var viewState = (object[])savedState; 
       fillReasons(); 
       base.LoadViewState(viewState[0]); 
      } 
      else 
      { 
       base.LoadViewState(savedState); 
      } 
     } 


     protected void Page_Load(object sender, EventArgs e) 
     { 

      if (!IsPostBack) 
      { 
       string callIDValue = Request.QueryString["CallID"]; 
       string callerIDValue = Request.QueryString["CallerID"]; 
       if (!String.IsNullOrEmpty(callerIDValue)) 
       { 
        callerID.Value = callerIDValue; 
        if (!String.IsNullOrEmpty(callIDValue)) 
        { 
         string query = "INSERT INTO Reason (callerID, callID, reason, timestamp) VALUES (@callerID, @callID, @reason, @timestamp)"; 
         SqlConnection con = getConnection(); 

         SqlCommand command = new SqlCommand(query, con); 
         command.Parameters.AddWithValue("@callerID", callerIDValue); 
         command.Parameters.AddWithValue("@callID", callIDValue); 
         command.Parameters.AddWithValue("@reason", defaultReason); 
         command.Parameters.AddWithValue("@timestamp", DateTime.Now.ToString()); 
         try 
         { 
          con.Open(); 
          command.ExecuteNonQuery(); 
          command.Dispose(); 
          con.Close(); 
         } 
         catch (Exception ee) 
         { 
          command.Dispose(); 
          con.Close(); 
          message.InnerHtml = ee.Message; 
         } 
        } 
        else 
        { 
         message.InnerHtml = "Call ID is empty"; 
        } 
       } 
       else 
       { 
        callerID.Value = "Undefined"; 
        message.InnerHtml = "Caller ID is empty"; 
       } 
       fillReasons(); 
      } 
      else 
      { 

      } 
     } 

     private void fillReasons() 
     { 

      string query = "SELECT * FROM wrapuplist WHERE isEnabled = @isEnabled"; 
      SqlConnection con = new SqlConnection(getConnectionString()); 
      SqlCommand cmd = new SqlCommand(query, con); 
      cmd.Parameters.AddWithValue("isEnabled", true); 
      con.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataTable results = new DataTable(); 
      da.Fill(results); 
      int numberOfReasons = 0; // a integer variable to know if the number of the reasons becomes able to be divided by four 
      HtmlGenericControl div = null; 
      foreach (DataRow row in results.Rows) 
      { 
       numberOfReasons++; 

       if ((numberOfReasons % 4) == 1) 
       { 
        div = new HtmlGenericControl("div"); 
        div.Attributes.Add("class", "oneLine"); 
       } 

       RadioButton radioButton = new RadioButton(); 
       radioButton.ID = "reason_" + row["reasonName"].ToString(); 
       radioButton.GroupName = "reason"; 
       radioButton.Text = row["reasonName"].ToString(); 

       div.Controls.Add(radioButton); 
       if (numberOfReasons % 4 == 0) 
       { 
        myValueDiv.Controls.Add(div); 
        //numberOfReasons = 0; 
       } 
       else if (numberOfReasons == results.Rows.Count) 
       { 
        myValueDiv.Controls.Add(div); 
        //numberOfReasons = 0; 
       } 
      } 
      cmd.Dispose(); 
      da.Dispose(); 
      con.Close(); 
     } 

     private SqlConnection getConnection() 
     { 
      return new SqlConnection(ConfigurationManager.ConnectionStrings["vmpcon"].ConnectionString); 
     } 

     private string getConnectionString() 
     { 
      return ConfigurationManager.ConnectionStrings["wrapupconnection"].ConnectionString.ToString(); 
     } 

     protected void buttonSaveClose_Click(object sender, EventArgs e) 
     { 
      var divcontrols = myValueDiv.Controls.OfType<HtmlGenericControl>(); 
      bool isFound = false; 
      RadioButton checkedRadioButton = null; 
      foreach (HtmlGenericControl loHTML in divcontrols) 
      { 
       var checkedRadioButtons = loHTML.Controls.OfType<RadioButton>().Where(radButton => radButton.Checked).ToList(); 
       foreach (RadioButton lobtn in checkedRadioButtons) 
       { 
        if (lobtn.Checked) 
        { 
         isFound = true; 
         checkedRadioButton = lobtn; 
        } 
       } 
      } 


      if (isFound) 
      { 
       sReasonError.InnerText = ""; 
       string reason = ""; 
       reason = checkedRadioButton.Text; 
       string callIDValue = Request.QueryString["CallID"]; 
       string callerIDValue = Request.QueryString["CallerID"]; 

       if (String.IsNullOrEmpty(callIDValue)) 
       { 
        message.InnerText = "Call ID is empty"; 
       } 
       else if (String.IsNullOrEmpty(callerIDValue)) 
       { 
        message.InnerText = "Caller ID is empty"; 
       } 
       else 
       { 
        message.InnerText = ""; 

        string query2 = "SELECT * FROM Reason WHERE callID = @callID AND reason != @reason"; 
        SqlConnection con = getConnection(); 
        SqlCommand command2 = new SqlCommand(query2, con); 
        command2.Parameters.AddWithValue("@callID", callIDValue); 
        command2.Parameters.AddWithValue("@reason", defaultReason); 
        con.Open(); 
        if (command2.ExecuteScalar() != null) 
        { 
         message.InnerText = "Already saved"; 
         command2.Dispose(); 
         con.Close(); 
        } 
        else 
        { 
         command2.Dispose(); 
         con.Close(); 
         string notes = taNotes.InnerText; 
         string query = "UPDATE Reason SET reason = @reason, notes = @notes, timestamp = @timestamp WHERE callID = @callID"; 
         SqlCommand command = new SqlCommand(query, con); 
         command.Parameters.AddWithValue("@callID", callIDValue); 
         command.Parameters.AddWithValue("@reason", reason); 
         command.Parameters.AddWithValue("@notes", notes); 
         command.Parameters.AddWithValue("@timestamp", DateTime.Now.ToString()); 
         try 
         { 
          con.Open(); 
          command.ExecuteNonQuery(); 
          command.Dispose(); 
          con.Close(); 
          message.InnerText = "Done Successfully"; 
          //ClientScript.RegisterStartupScript(typeof(Page), "closePage", "<script type='text/JavaScript'>window.close();</script>"); 
          ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.open('close.html', '_self', null);", true); 
         } 
         catch (Exception ee) 
         { 
          command.Dispose(); 
          con.Close(); 
          message.InnerText = "Error, " + ee.Message; 
         } 
        } 
       } 
      } 
      else 
      { 
       sReasonError.InnerText = "Required"; 
       message.InnerText = "Select a reason"; 
       //fillReasons(); 
      } 
     } 

    } 

如你所見,所有的連接都被關閉,我沒做什麼不對嗎?

+0

所述一個,而不是調用'處分與Close'明確..重新寫使用'使用(){}'這將處理代碼自動設置爲你..也是什麼命令.TimeOut值設置爲..? – MethodMan 2015-04-02 19:52:36

+0

@MethodMan我沒有設置這個'commad.TimeOut'的東西,我給你我所有的代碼 – 2015-04-02 20:23:35

+0

@MethodMan你能給我一個關於在我的代碼中使用'using'的例子嗎? – 2015-04-02 20:26:01

回答

0

在使用try catch時,關閉連接和處置應該在finally塊中。

,或者使用一個塊使用象下面 using(SqlConnection con = getConnection()) { con.Open(); //Do your operation here. The connection will be closed and disposed automatically when the using scope is exited }

相關問題