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 = "用户名不存在!";
}
} }
错误提示出现在红字那里
{
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.RowsCount>0)
if (this.TextBoxloginname.Text.Trim() == read["username"].ToString().Trim())
if(conn.State==ConnectionState.Open)
{
SqlDataReader read = sqlcom.ExecuteReader();
........
}另外,你数据库里,“username”是否真的有值?
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 = "您输入的密码有误,请重新输入.";
}
}
}
if(myReader.HasRows)
{
//循环读取记录
while(myReader.Read())
{
this.Labeltext.Text = myReader.GetString(0);
}
}