2015-03-02 36 views
-1
protected void btn_search_Click(object sender, EventArgs e) 
    { 
     using (con = new SqlConnection(CS)) 
     { 
      string _var_search = ddl_search_by.SelectedItem.Text; 
      string _var_by = ddl_search.SelectedItem.Text; 
      cmd = new SqlCommand("Select * from UserProfile Where '"+_var_search+"'='" + _var_by + "'", con); 

      da = new SqlDataAdapter(cmd); 
      ds = new DataSet(); 
      da.Fill(ds); 
      Repeater1.DataSource = ds.Tables[0]; 
      Repeater1.DataBind(); 
      if (ds.Tables[0].Rows.Count > 0) 
      { 
       txt_not_found.Visible = false; 
      } 
      else 
      { 
       txt_not_found.Visible = true; 
      } 
     } 
    } 
+1

_Questions寻求帮助调试(“为什么不是这个代码工作?“)必须包括**期望的行为,特定的问题或错误**以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用._ – cubrr 2015-03-02 09:54:10

+0

我同意这个问题目前还不清楚(究竟发生了什么?实际发生了什么?)。而且:**不要将输入值连接成SQL **。 – 2015-03-02 09:57:34

回答

1

不使用引号在第一个参数下拉菜单中的SQL语句:

cmd = new SqlCommand("Select * from UserProfile Where "+_var_search+"='" + _var_by + "'", con); 

更好地利用PARAMATERS:

cmd = new SqlCommand("Select * from UserProfile Where "+_var_search+"[email protected]", con); 
cmd.Parameters.AddWithValue("@param", _var_by); 
+0

(也应该使用白名单来反映'_var_search'的值) – 2015-03-02 09:58:42