GridView不显示数据

时间:2015-12-07 06:46:49

标签: asp.net

我的代码是关于在Exp2数据库中搜索Expression并在GridView中显示结果 问题是当我按下搜索按钮时,GridView没有显示 你能告诉我为什么会这样吗?

protected void Button1_Click2(object sender, EventArgs e)
{ 
 SqlConnection conn = new SqlConnection(@"Data Source=.\SQLExpress;Persist Security Info=True;Integrated Security=SSPI ; Initial Catalog=aspSPM_Exp2");
 string str = @"select * from [exp] where  (@search = Expression) Order by Expression";
 using (SqlCommand com = new SqlCommand(str, conn))
 {
  com.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox1.Text;
  conn.Open();
  SqlDataAdapter da = new SqlDataAdapter(com);
  DataSet ds = new DataSet ();
  da.Fill(ds,"Expression");
  conn.Close();
  GridView1.DataSource = ds;
  GridView1.DataBind();
 }
}

1 个答案:

答案 0 :(得分:1)

你的select语句应该是这样的

string str = @"select * from [exp] where (Expression = @search) Order by Expression";

目前您以Parameter传递的内容被视为Column的{​​{1}}而Table将不会Table,因此这将选择否Column和gridview将为空。