2016-04-30 102 views
1

我想通過使用它的名字以顯示從數據庫中的GridView的項目信息在另一頁的內容,我想在GridView到另一個網頁 我想這代碼,,但它並沒有在第一頁按名稱搜索項目,並顯示

protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    public string txt 
    { 
     get 
     { 
      // Return the actual name if it is not null. 
      return TextBox1.Text ?? string.Empty; 
     } 

    } 
    } 

在第二頁

protected void Page_Load(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=FATIMAH;Initial Catalog=makeup;Integrated Security=True"); 
     string find = "select * from product where(name like '%' [email protected]+ '%')"; 
     SqlCommand comm = new SqlCommand(find, con); 

     comm.Parameters.Add("@name", SqlDbType.NChar).Value = txt; 
     con.Open(); 
     comm.ExecuteNonQuery(); 
     SqlDataAdapter da = new SqlDataAdapter(); 
     da.SelectCommand = comm; 
     DataSet ds = new DataSet(); 
     da.Fill(ds, "name"); 
     GridView1.DataSource = ds; 
     GridView1.DataBind(); 
     con.Close(); 
    } 
} 

回答

0

你可以傳遞參數使用查詢字符串另一頁。的onclick第一頁上的按鈕的情況下做到這一點: -

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string search_word = TextBox1.Text.ToString(); 
     Response.Redirect("~/secondpage.aspx?srch_word=" + search_word); 
    } 

並在第二頁的請求查詢字符串: -

protected void Page_Load(object sender, EventArgs e) 
    { 
     string search = Request.QueryString["srch_word"]; 
     //execute sql query to perform search operation 
    } 
0

您的查詢似乎離我上班

。嘗試"select * from product where name like '%@name%'"

也爲您的參數,你可以cmd.Parameters.AddWithValue("name", nameVariable);

不知道爲什麼你需要指定在這種情況下的類型。

0

你的問題不是SQL查詢,你的問題是參數傳遞到另一個頁面。 因爲這個原因,你可以做到這一點在至少4種不同的方式。通過Cookie的

  1. 通過查詢字符串發送文本
  2. 通過郵政傳遞數據是
  3. 它傳遞
  4. 通過會話

在這種情況下,你可以使用查詢字符串發送,但你必須關心安全問題。 順便說一句,這取決於你如何重定向到第2頁。