超时已过期。从池中获取连接之前经过的超时时间。

时间:2015-04-02 19:50:14

标签: c# asp.net .net

我收到此错误

  

超时已过期。从池中获取连接之前经过的超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。

我读了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();
            }
        }

    }

如您所见,所有连接都已关闭,我做错了什么?

1 个答案:

答案 0 :(得分: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 }