C# 超时已过期。从池中获取连接之前经过的超时时间。
我得到了这个错误 超时已过期。从池中获取连接之前经过的超时时间。发生这种情况的原因可能是所有池连接都在使用中,并且已达到最大池大小 我读到,它声明异常是由于未关闭连接而导致的。但是,我关闭代码中的所有连接 这是我的代码,很简单C# 超时已过期。从池中获取连接之前经过的超时时间。,c#,asp.net,.net,C#,Asp.net,.net,我得到了这个错误 超时已过期。从池中获取连接之前经过的超时时间。发生这种情况的原因可能是所有池连接都在使用中,并且已达到最大池大小 我读到,它声明异常是由于未关闭连接而导致的。但是,我关闭代码中的所有连接 这是我的代码,很简单 public partial class index : System.Web.UI.Page { private static string defaultReason = "reason not selected"; pr
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();
}
}
}
公共部分类索引:System.Web.UI.Page
{
私有静态字符串defaultReason=“未选择原因”;
受保护的覆盖对象SaveViewState()
{
//添加动态控件后立即保存视图状态
var viewState=新对象[1];
viewState[0]=base.SaveViewState();
返回视图状态;
}
受保护的覆盖无效LoadViewState(对象保存状态)
{
//加载数据frm保存的视图状态
if(savedState是对象[]&((对象[])savedState.Length==1)
{
var viewState=(object[])savedState;
填写理由();
base.LoadViewState(viewState[0]);
}
其他的
{
base.LoadViewState(savedState);
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
string callIDValue=Request.QueryString[“CallID”];
字符串callerIDValue=Request.QueryString[“CallerID”];
如果(!String.IsNullOrEmpty(callerIDValue))
{
callerID.Value=callerIDValue;
如果(!String.IsNullOrEmpty(callIDValue))
{
string query=“插入Reason(callerID,callID,Reason,timestamp)值(@callerID,@callID,@Reason,@timestamp)”;
SqlConnection con=getConnection();
SqlCommand=newsqlcommand(查询,con);
command.Parameters.AddWithValue(“@callerID”,callerIDValue);
command.Parameters.AddWithValue(“@callID”,callIDValue);
command.Parameters.AddWithValue(“@reason”,defaultReason);
command.Parameters.AddWithValue(“@timestamp”,DateTime.Now.ToString());
尝试
{
con.Open();
command.ExecuteNonQuery();
command.Dispose();
con.Close();
}
捕获(异常ee)
{
command.Dispose();
con.Close();
message.InnerHtml=ee.message;
}
}
其他的
{
message.InnerHtml=“呼叫ID为空”;
}
}
其他的
{
callerID.Value=“未定义”;
message.InnerHtml=“呼叫者ID为空”;
}
填写理由();
}
其他的
{
}
}
私人理由()
{
string query=“从包装列表中选择*,其中isEnabled=@isEnabled”;
SqlConnection con=新的SqlConnection(getConnectionString());
SqlCommand cmd=新的SqlCommand(查询,con);
cmd.Parameters.AddWithValue(“isEnabled”,true);
con.Open();
SqlDataAdapter da=新的SqlDataAdapter(cmd);
DataTable结果=新DataTable();
da.填写(结果);
int numberOfReasons=0;//一个整数变量,用于知道原因数是否可以被四除
HtmlGenericControl div=null;
foreach(results.Rows中的DataRow行)
{
numberOfReasons++;
如果((NumberOfReasions%4)==1)
{
div=新的HtmlGenericControl(“div”);
添加(“类”、“单行”);
}
RadioButton RadioButton=新RadioButton();
radioButton.ID=“reason_”+行[“reasonName”].ToString();
radioButton.GroupName=“原因”;
radioButton.Text=行[“reasonName”].ToString();
div.Controls.Add(单选按钮);
如果(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();
}
私有SqlConnection getConnection()
{
返回新的SqlConnection(ConfigurationManager.ConnectionString[“vmpcon”].ConnectionString);
}
私有字符串getConnectionString()
{
返回ConfigurationManager.ConnectionString[“wrapupconnection”].ConnectionString.ToString();
}
受保护的无效按钮取消单击(对象发送者,事件参数e)
{
var divcontrols=myValueDiv.Controls.OfType();
bool isFound=false;
RadioButton checkedRadioButton=null;
foreach(divcontrols中的HtmlGenericControl loHTML)
{
var checkedRadioButtons=loHTML.Controls.OfType().Where(radButton=>radButton.Checked.ToList();
foreach(选中RadioButtons中的RadioButton窗口)
{