小白遇到问题了,数据连接成功的,但是点击确定键后不能重置密码,到底是哪里的问题啊!代码和图片如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
namespace WebApplication2
{
public partial class 找回密码 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["username"] == null)
{
Response.Redirect("登录.aspx");
}
}
lblName.Text = Request.QueryString["username"];
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
//声明Conn为一个SQL Server连接对象
SqlConnection Conn = new SqlConnection(connString);
Conn.Open(); //打开连接
string SelectSql = "select * from 用户表 where 用户名='" + lblName.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(); //创建DataAdapter对象
da.SelectCommand = new SqlCommand(SelectSql, Conn);
DataSet ds = new DataSet(); //创建一个空DataSet对象
//将DataAdapter执行SQL语句返回的结果填充到DataSet对象
da.Fill(ds);
Conn.Close();
if (ds.Tables[0].Rows.Count == 0)
{
Response.Write("<script language=javascript>alert('用户名不存在!');</script>");
return;
}
DataRow MyRow = ds.Tables[0].Rows[0];
lblQuestion.Text = MyRow[3].ToString().Trim();
}
protected void lbtnOK_Click(object sender, EventArgs e)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
//声明Conn为一个SQL Server连接对象
SqlConnection Conn = new SqlConnection(connString);
Conn.Open(); //打开连接
string SelectSql = "select * from 用户表 where 用户名='" + lblName.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(); //创建DataAdapter对象
da.SelectCommand = new SqlCommand(SelectSql, Conn);
DataSet ds = new DataSet(); //创建一个空DataSet对象
//将DataAdapter执行SQL语句返回的结果填充到DataSet对象
da.Fill(ds);
string SecAnswer = FormsAuthentication.HashPasswordForStoringInConfigFile(textAnswer.Text, "MD5");
DataRow MyRow = ds.Tables[0].Rows[0];
if (SecAnswer == MyRow[4].ToString().Trim())
{
SqlCommandBuilder scb = new SqlCommandBuilder(da);
Random r = new Random();
string NewPwd = r.Next(100000, 999999).ToString();
Response.Write("<script language=javascript>alert('你的新密码是:" + NewPwd + ",请及时更改!');</script>");
NewPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(NewPwd, "MD5");
MyRow["密码"] = NewPwd;
da.Update(ds); //将DataSet中数据变化提交到数据库(更新数据库)
Conn.Close();
}
else
{
Response.Write("<script language=javascript>alert('安全问题答案错!');</script>");
}
}
protected void lbtnBack_Click(object sender, EventArgs e)
{
Response.Redirect("登录.aspx");
}
}
}