string checkuserQuery = "select username from usersign where Username=' " + TextBox1.Text + " ' ";
SqlCommand usercom = new SqlCommand(checkuserQuery, conn);
string user1 = string.Empty;
Object val = usercom.ExecuteScalar();
if (val != null)
{
user1 = val.ToString();
if (user1 == TextBox1.Text)
{
string checkpasswordQuery = "select password from usersign where Username=' " + TextBox1.Text + " ' ";
SqlCommand passcom = new SqlCommand(checkpasswordQuery, conn);
string password = passcom.ExecuteScalar().ToString();
if (password == TextBox2.Text)
{
Session["New"] = TextBox1.Text;
Label5.Text = "password is correct";
Response.Redirect("user.aspx");
}
else
{
Label5.Text = "password is not correct";
}
}
}
else
{
Label5.Text = "val is null";
}
}发布于 2014-05-01 15:24:22
如果查询没有返回值,ExecuteScalar()将返回null。
返回结果集中第一行的第一列,如果结果集为空,则返回空引用(在Visual Basic中为Nothing)。
Source
此行将抛出空引用异常:
passcom.ExecuteScalar().ToString();使用字符串连接构建查询很容易出错。更重要的是,它容易受到SQL注入的攻击。代码建议密码以纯文本形式存储在数据库中。
对于 any 应用程序来说,SQL注入和纯文本密码是一个严重的问题。参数化您的查询(使用ADO.Net非常容易)并散列您的密码。
缺少匹配项可能是由以下代码行引起的:
string checkpasswordQuery = "select password from usersign where Username=' " + TextBox1.Text + " ' ";请注意字符串连接中添加的额外空格。无论TextBox1中是什么,前面/后面都会有空格,这会导致匹配失败。
发布于 2014-05-01 15:23:58
问题可能是下面的空格字符(我在空格使用不正确的地方放了一个*)
where Username='*" + TextBox1.Text + "*' "因此,上面的代码意味着您的查询正在尝试获取开头和结尾都有空格字符的用户名,因此只需删除这些空格
另一点是,这种查询应该与参数一起使用,因为它容易受到SQL注入类型的攻击
https://stackoverflow.com/questions/23403640
复制相似问题