已有打开的与此命令相关联的 DataReader,必须首先将它关闭——什么原因啊???
提示错误如下:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。源错误:
行 40: string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
行 41: SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
行 42: cmd1.ExecuteNonQuery();
行 43:
行 44: Response.Write("<script>alert('添加成功!')</script>");
源文件: f:\wyx3.0\wyxback\admin_adminadd.aspx.cs 行: 42
源码: protected void Button1_Click(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();
SqlConnection ConnSql = new SqlConnection(strConn);
ConnSql.Open();
string strSQL = "SELECT * FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand(strSQL, ConnSql);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())//如果用户名存在
{
Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>"); }
else
{
string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5"); string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
cmd1.ExecuteNonQuery(); Response.Write("<script>alert('添加成功!')</script>");
} dr.Close();
ConnSql.Close();
Response.Redirect("admin_admin.aspx");//返回管理页面 }
说明一下,这个程序的功能是添加管理员数据库里的管理员表是wyx_user我估计是if语句有问题,因为要考虑到如果添加的用户名已经存在,就要给出提示信息
提示错误如下:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。源错误:
行 40: string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
行 41: SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
行 42: cmd1.ExecuteNonQuery();
行 43:
行 44: Response.Write("<script>alert('添加成功!')</script>");
源文件: f:\wyx3.0\wyxback\admin_adminadd.aspx.cs 行: 42
源码: protected void Button1_Click(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();
SqlConnection ConnSql = new SqlConnection(strConn);
ConnSql.Open();
string strSQL = "SELECT * FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand(strSQL, ConnSql);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())//如果用户名存在
{
Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>"); }
else
{
string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5"); string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
cmd1.ExecuteNonQuery(); Response.Write("<script>alert('添加成功!')</script>");
} dr.Close();
ConnSql.Close();
Response.Redirect("admin_admin.aspx");//返回管理页面 }
说明一下,这个程序的功能是添加管理员数据库里的管理员表是wyx_user我估计是if语句有问题,因为要考虑到如果添加的用户名已经存在,就要给出提示信息
解决方案 »
- KooBoo_cms GetContentsByCategory 如何使用(或文章表中根据类型显示该类型的内容项[含前几条数据])
- nhibernate 怎么对没有id的表进行映射
- 求够天轰川 川哥asp.net2.0视频
- ASP.NET用户登陆问题
- 请问CreateUserWizard控件推荐使用吗?
- 在.cs中如何引用ID
- 在iis中,我的默认web站点就是启动不了了呢???
- 初学asp.net,请指教Render、OnPreRender、RenderChildren的意思和作用
- 求助:ajaxcontrol toolkit页面出错-“ajaxcontroltoolkit未定义”
- LinkButton表现为无法点击
- JS中如何把参数传到后台的程序中?
- 谁有免费的报表控件?
string strSQL = "SELECT * FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";
改为:string strSQL = "SELECT COUNT(*) FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";然后设一个int 变量,int count=cmd.ExecuteNonQuery();
把if语句的条件改成if(count>0).就是说如果用户名存在的话.这样就行了.
大概就是这样!
你是说这样写:
if (dr.Read())//如果用户名存在
{
dr.Close();
Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>"); }
{
dr.Close();
Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>"); }
//...前面都一样
SqlDataReader dr = cmd.ExecuteReader();
boolean bExists;
bExists = dr.Read();//如果用户名存在
dr.close();
if (bExists)
{...}
//后面都一样。
{
Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>"); }
else
{
string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5"); string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
cmd1.ExecuteNonQuery(); Response.Write("<script>alert('添加成功!')</script>");
} dr.Close();
这样写
{
Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>"); }
else
{
dr.Close();
string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5"); string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
cmd1.ExecuteNonQuery(); Response.Write("<script>alert('添加成功!')</script>");
}
这样写,写到else里边的第一句!
反正你用了sqldatareader了就在用过后第一时间关闭
为何不用SqlCommand.ExecuteScalar 方法呢if(cmd.ExecuteScalar() == null) // 没有存在的用户
{
//插入新用户
}
else
{
//提示已存在
}// 返回