基本的にこのコードを実行すると、次のエラーが発生し、解決方法がわかりません。ボタンをクリックするとこのエラーが表示され、Visual Studioにリダイレクトされて次のエラーが表示されます。私はこのエラーを解決するのを助けてください、助けに感謝します。A {"このコマンドに関連付けられた開いているDataReaderがあり、最初に閉じる必要があります。"}
このコマンドに関連付けられている開いているDataReaderが既にあります。これは最初に閉じる必要があります。
public partial class forgot : System.Web.UI.Page
{
protected void resetpass_Click(object sender, EventArgs e)
{
SqlDataReader reader = null;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from reg where Username [email protected]", con);
cmd.Parameters.AddWithValue("@username", username.Text);
cmd.Parameters.AddWithValue("@security1", sec1.Text);
cmd.Parameters.AddWithValue("@security2", sec2.Text);
reader = cmd.ExecuteReader();
if (reader != null && reader.HasRows)
{
newpass.Visible = true;
confpass.Visible = true;
Label1.Text = "New Password";
Label2.Text = "Confirm New Password";
SqlDataAdapter updates = new SqlDataAdapter("update reg set Password='" + newpass.Text + "'", con);
DataSet ds = new DataSet();
updates.Fill(ds);
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('The Password has been Changed')</script>");
con.Close();
}
}
}
'Fill'方法が自動的に' open'接続、およびあなたは 'Close'前回接続する必要があることの前に:この
変更。 –
[SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - SQLステートメントを連結しない** ** ** SQL注入を避けるために、代わりにパラメータ化されたクエリ** –
あなたのコードを編集しているのはなぜですか? –