非布尔类型指定的异常

时间:2014-02-21 11:21:44

标签: c# sql-server-2012

con.Open();
string str = "select Email ID,Type from User_Reg where Email ID=@EmailID and password=@password";
SqlCommand cmd = new SqlCommand(str,con);
cmd.Parameters.AddWithValue("@EmailID", txtemail.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
    Response.Redirect("Home.aspx");
}

这是我在c#中的代码。我在da.fill(dt);上收到此错误:

An expression of non-boolean type specified in a context where a condition is expected, near 'ID'

我正在使用带有内置sql server 2012的visual studio 2012。

2 个答案:

答案 0 :(得分:0)

问题出在where子句中,删除别名 :

select Email ID,Type from User_Reg where Email ID=@EmailID and password=@password

应该是这样的

select Email ID,Type from User_Reg where Email=@EmailID and password=@password

答案 1 :(得分:0)

您的sql语句为false:

WRONG:

string str =“选择电子邮件ID,从User_Reg输入,其中电子邮件ID = @EmailID和密码= @密码”;

TRUE:

string str =“select EmailID,从User_Reg输入,其中EmailID = @ EmailID和密码= @密码”;