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();
}