在没有任何数据时进行无效的读取尝试。 错误求解答
protected void Button1_Click(object sender, EventArgs e)//登录界面用户的登录,验证码验证。{
string sqltext = "select * from 用户注册信息";
SqlConnection conn = new SqlConnection("server=SILENT-PC\\SILENT;database=阳光传媒用户数据库;integrated security=true");
conn.Open();
SqlCommand sqlcom = new SqlCommand(sqltext,conn);
SqlDataReader read = sqlcom.ExecuteReader();
if(read.HasRows)
{
if (this.TextBoxloginname.Text.Trim() == read["username"].ToString().Trim())
{
if (this.TextBoxloginpassword.Text.Trim() == read["password"].ToString().Trim())
{
conn.Close();
Response.Redirect("用户登陆成功.aspx");
}
else
{
conn.Close();
this.Labeltext.Text = "您输入的密码有误,请重新输入.";
}
}
else
{
conn.Close();
this.Labeltext.Text = "用户名不存在!";
}
}
}
错误提示出现在红字那里 --------------------编程问答-------------------- 没人? --------------------编程问答-------------------- if(read.HasRows)
改为下面的看一下
if(read.RowsCount>0)
--------------------编程问答-------------------- 加上 read.Read();
if (this.TextBoxloginname.Text.Trim() == read["username"].ToString().Trim()) --------------------编程问答-------------------- read.HasRows改成read.Read() --------------------编程问答-------------------- conn.Open();
if(conn.State==ConnectionState.Open)
{
SqlDataReader read = sqlcom.ExecuteReader();
........
}
另外,你数据库里,“username”是否真的有值?
--------------------编程问答--------------------
SqlDataReader read = sqlcom.ExecuteReader();--------------------编程问答--------------------
if(read.HasRows)
{
while(read.read())
{
if (this.TextBoxloginname.Text.Trim() == read["username"].ToString().Trim())
{
if (this.TextBoxloginpassword.Text.Trim() == read["password"].ToString().Trim())
{
conn.Close();
Response.Redirect("用户登陆成功.aspx");
}
else
{
conn.Close();
this.Labeltext.Text = "您输入的密码有误,请重新输入.";
}
}
}
SqlDataReader myReader= sqlcom.ExecuteReader();
//判断是否存在记录
if(myReader.HasRows)
{
//循环读取记录
while(myReader.Read())
{
this.Labeltext.Text = myReader.GetString(0);
}
}
补充:.NET技术 , C#