[英]Simple update query caused System.ComponentModel.Win32Exception: The wait operation timed out error in C# and SQL Server
I'm trying to create a Forgot password page, that verify the user's username, email, security question and answer, before sending them to the new password page.我正在尝试创建一个忘记密码页面,用于验证用户的用户名 email、安全问题和答案,然后再将其发送到新密码页面。 The code below is the onClick that redirect user to the new password page.下面的代码是将用户重定向到新密码页面的 onClick。
protected void SubmitButton_Click(object sender, EventArgs e)
{
string email = Email.Text;
string user = UserName.Text;
string question = Question.Text;
string answer = Answer.Text;
string strCon = ConfigurationManager.ConnectionStrings["WebConfigConString"].ConnectionString;
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strCheck = "SELECT * FROM ACCOUNT WHERE ID = @id AND EMAIL = @email AND SECURITYQUESTION = @question AND SECURITYANSWER = @answer";
SqlCommand cmdCheck = new SqlCommand(strCheck, con);
cmdCheck.Parameters.AddWithValue("@id", user);
cmdCheck.Parameters.AddWithValue("@question", question);
cmdCheck.Parameters.AddWithValue("@email", email);
cmdCheck.Parameters.AddWithValue("@answer", answer);
SqlDataReader dtrCheck = cmdCheck.ExecuteReader();
if (dtrCheck.HasRows)
{
Response.Redirect("newPassword.aspx?id=" + user);
}
else
{
ErrorMsg.Text = "Invalid username or email / question and answer does not match!";
}
}
And this code below is the form segment of the aspx page for newPassword.下面这段代码是newPassword的aspx页面的表单段。
<form runat="server">
<div class="form-group">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">
Password:</asp:Label>
<asp:TextBox class="form-control form-control-user" ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ForeColor="Red">*</asp:RequiredFieldValidator>
</div>
<div class="form-group">
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">
Confirm Password:</asp:Label>
<asp:TextBox class="form-control form-control-user" ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
ErrorMessage="Confirm Password is required." ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
ControlToValidate="ConfirmPassword" ErrorMessage="The Password and Confirmation Password must match." ForeColor="Red">*</asp:CompareValidator>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
<asp:Button ID="SubmitButton" runat="server" Text="Confirm" class="btn btn-primary btn-user btn-block" OnClick="SubmitButton_Click"/>
</form>
And this is the codebehind onclick
function for newPassword:这是新密码的 onclick onclick
后面的代码:
protected void SubmitButton_Click(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string password = Password.Text;
string strCon = ConfigurationManager.ConnectionStrings["WebConfigConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strCon))
{
con.Open();
string strChange = "UPDATE ACCOUNT SET PASSWORD = @password WHERE ID = @id";
SqlCommand cmdChange = new SqlCommand(strChange, con);
cmdChange.Parameters.AddWithValue("@id", id);
cmdChange.Parameters.AddWithValue("@password", password);
cmdChange.ExecuteNonQuery();
con.Close();
Response.Redirect("login.aspx?msg=Password updated successfully!");
}
}
However, whenever I click submit after filling out the new password, it freeze for a long time then display this error:但是,每当我在填写新密码后单击提交时,它会冻结很长时间然后显示此错误:
System.ComponentModel.Win32Exception: The wait operation timed out error System.ComponentModel.Win32Exception:等待操作超时错误
Apparently, I forgot to close the connection before redirecting the user to the newPassword.aspx, simply add a con.Close() before redirecting worked for me.显然,我忘记在将用户重定向到 newPassword.aspx 之前关闭连接,只需在重定向对我有用之前添加一个 con.Close() 即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.