简单更新查询导致System.ComponentModel.Win32异常:C#和SQL Server中的等待操作超时错



我正在尝试创建一个Forgot密码页面,在将用户的用户名、电子邮件、安全问题和答案发送到新密码页面之前,该页面将进行验证。下面的代码是将用户重定向到新密码页面的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!";
}
}

下面的代码是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>

这是newPassword:的codeehindonclick函数

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!");
}
}

然而,每当我在填写新密码后点击提交时,它会冻结很长一段时间,然后显示此错误:

System.ComponentModel.Win32异常:等待操作超时错误

显然,在将用户重定向到newPassword.aspx之前,我忘记了关闭连接,只需在重定向之前添加一个con.close((就可以了。

相关内容

最新更新