C# 在预期条件的上下文中指定的非布尔类型的表达式,靠近“ID”
有人能指出是什么导致了这个错误吗?我一直想把它修好,但失败了。我不明白“ID”有什么问题。。。到底是哪个部分产生了这个错误 这是我的密码:C# 在预期条件的上下文中指定的非布尔类型的表达式,靠近“ID”,c#,asp.net,visual-studio,C#,Asp.net,Visual Studio,有人能指出是什么导致了这个错误吗?我一直想把它修好,但失败了。我不明白“ID”有什么问题。。。到底是哪个部分产生了这个错误 这是我的密码: protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = SqlDataSource1.ConnectionString; string st
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = SqlDataSource1.ConnectionString;
string str = "SELECT * FROM Student "
+ " WHERE Student ID = '" + StudentID.Text + "' AND "
+ " Password = '" + SPassword.Text + "'";
SqlCommand cmdSelect = new SqlCommand(str, conn);
SqlDataReader reader;
conn.Open();
reader = cmdSelect.ExecuteReader();
if (reader.Read())
{
if (StudentID.Text == "900000000")
Response.Write("<body onload=\"window.open('Admin.aspx', '_top')\"></body>");
else
Response.Write("<body onload=\"window.open('user.aspx', '_top')\"></body>");
}
else
lblMsg.Text = "Invalid Username and/or Password, please re-try!!";
conn.Close();
}
从Student.ID所在的学生中选择*。。。你有一个空格而不是一个点
您可以完全忽略该学生,只需使用ID作为密码。在我修复它之后。。出现一个新错误,显示无效的列名“StudentID”。原因可能是什么?尽管更正,但这不是一个好建议-OP需要使用参数化查询来构造SQL请求。您试图从DB中提取的ID列的名称是什么?-这就是您在SELECT语句中需要的内容。@John3136它的名称是'StudentID。。在SELECT语句中也是如此。@AlexeiLevenkov我不知道,这就是我在大学里学习它的方式。请你提供一个资源,教你的建议?谢谢。使用SQL注入显式编写代码不是一个好主意。请检查使用参数化查询的正确方法。