在应使用条件的上下文(在 ✀✀ 附近)中指定了非布尔类型的表达式

2024-12-24 14:32:49
推荐回答(1个)
回答1:

strsql = "select * from Sheet2$ where '"+Convert.ToString( Session["sql"] )+ "'";
你这里的Where子句后面是一个字符串,而正确的应该是一个返回布尔值的表达式。换成下面的:
protected void Button2_Click(object sender, EventArgs e)
{

string Sql = " ";
if (CheckBox1.Checked)
{

Sql = " bm = '" +this.bm.Text.Trim() + "'";

}

Session["sql"] = Sql;
this.bind();
}
public void bind()
{
string strsql;

strsql = "select * from Sheet2$ where "+Convert.ToString( Session["sql"] );
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
SqlDataAdapter sda = new SqlDataAdapter(strsql, strcon);
DataSet ds = new DataSet();
sda.Fill(ds, "Sheet2$");
GridView1.DataSource = ds.Tables["search"];
GridView1.DataSource = ds;

GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
}