asp.net 关于关闭 DataReader 的问题
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class reg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
name.Text = "";
password.Text = "";
password2.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
if(name.Text == "" || password.Text == "" || password2.Text =="" )
{
Response.Write("<script>alert('这里的信息都要填写!');history.go(-1)</script>");
}
else
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/no1.mdb");
OleDbConnection myConn = new OleDbConnection(strConnection);
myConn.Open();
String sq = "select * from [user] where userid='" + name.Text + "'";
OleDbCommand cmd = new OleDbCommand(sq, myConn);
OleDbDataReader da = cmd.ExecuteReader();
if (da.Read())
{
Response.Write("<script>alert('用户已经存在');history.go(-1)</script>");
}
else if (password.Text != password2.Text)
{
Response.Write("<script>alert('两次密码不相同');history.go(-1)</script>");
}
else
{
String sq2 = "insert into [user] (userid,password) values ('" + name.Text + "','" + password.Text + "')";
cmd.CommandText = sq2;
cmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功');window.window.location.href='Default.aspx';</script>");
}
myConn.Close();
}
}
}
错误提示: System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
行 51: String sq2 = "insert into [user] (userid,password) values ('" + name.Text + "','" + password.Text + "')";
行 52: cmd.CommandText = sq2;
行 53: cmd.ExecuteNonQuery();
行 54: Response.Write("<script>alert('注册成功');window.window.location.href='Default.aspx';</script>");
行 55: }
53行出错
请高手解答... 展开
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class reg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
name.Text = "";
password.Text = "";
password2.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
if(name.Text == "" || password.Text == "" || password2.Text =="" )
{
Response.Write("<script>alert('这里的信息都要填写!');history.go(-1)</script>");
}
else
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/no1.mdb");
OleDbConnection myConn = new OleDbConnection(strConnection);
myConn.Open();
String sq = "select * from [user] where userid='" + name.Text + "'";
OleDbCommand cmd = new OleDbCommand(sq, myConn);
OleDbDataReader da = cmd.ExecuteReader();
if (da.Read())
{
Response.Write("<script>alert('用户已经存在');history.go(-1)</script>");
}
else if (password.Text != password2.Text)
{
Response.Write("<script>alert('两次密码不相同');history.go(-1)</script>");
}
else
{
String sq2 = "insert into [user] (userid,password) values ('" + name.Text + "','" + password.Text + "')";
cmd.CommandText = sq2;
cmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功');window.window.location.href='Default.aspx';</script>");
}
myConn.Close();
}
}
}
错误提示: System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
行 51: String sq2 = "insert into [user] (userid,password) values ('" + name.Text + "','" + password.Text + "')";
行 52: cmd.CommandText = sq2;
行 53: cmd.ExecuteNonQuery();
行 54: Response.Write("<script>alert('注册成功');window.window.location.href='Default.aspx';</script>");
行 55: }
53行出错
请高手解答... 展开
2个回答
展开全部
if (da.Read())
{
Response.Write("<script>alert('用户已经存在');history.go(-1)</script>");
}
else if (password.Text != password2.Text)
{
Response.Write("<script>alert('两次密码不相同');history.go(-1)</script>");
}
else
{
改成
if (da.Read())
{
myConn.Close();
Response.Write("<script>alert('用户已经存在');history.go(-1)</script>");
}
else if (password.Text != password2.Text)
{
myConn.Close();
Response.Write("<script>alert('两次密码不相同');history.go(-1)</script>");
}
else
{
myConn.Close();
{
Response.Write("<script>alert('用户已经存在');history.go(-1)</script>");
}
else if (password.Text != password2.Text)
{
Response.Write("<script>alert('两次密码不相同');history.go(-1)</script>");
}
else
{
改成
if (da.Read())
{
myConn.Close();
Response.Write("<script>alert('用户已经存在');history.go(-1)</script>");
}
else if (password.Text != password2.Text)
{
myConn.Close();
Response.Write("<script>alert('两次密码不相同');history.go(-1)</script>");
}
else
{
myConn.Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询