2013-06-11 134 views
0

我試圖做一個基於DropDownList中選定值的鍛鍊過濾器GridView。 我有一個在下拉列表中的名稱列表,當我點擊名字時,我想讓人的信息出現在datagridview中。 我試着這樣做是這樣的:基於dropdownlist選擇值的filter gridview

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 

{ 

OleDbConnection con = new OleDbConnection("Data Source=orcl;Persist Security Info=True;User ID=eq;Password=*******;Unicode=True"); 
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(String.Format("select * from teacher where idteacher={0} ", idteacher), con); 

DataTable table = new DataTable(); 
dataAdapter.Fill(table); 
GridView1.DataSource = table; 

} 

,但它不工作,而「數據表」對象不能被識別買C#。 任何人都可以幫助我看到我做錯了什麼? 謝謝! 改善問題固定鏈接發佈時間7分

+0

這裏是否有錯誤? –

回答

0

您未顯示初始化的地方idTeacher

但是,你必須DataBind在ASP.NET中GridView已分配後DataSource

GridView1.DataSource = table; 
GridView1.DataBind(); // <-- this is missing in your code 

你無論如何都應該使用SQL參數,否則你是潛在的開放SQL注入,你應該使用using聲明,以確保所有非託管資源配置(甚至在錯誤):

using (var con = new OleDbConnection("ConnectionString")) 
using (var da = new OleDbDataAdapter("select * from teacher where [email protected]", con)) 
{ 
    da.SelectCommand.Parameters.AddWithValue("@idteacher", idTeacher); 
    DataTable table = new DataTable(); 
    da.Fill(table); // opening or closing connection not needed, done by Fill 
    GridView1.DataSource = table; 
    GridView1.DataBind(); 
} 
+0

謝謝..我忘了databind()。但數據表的對象仍然不被識別爲C#...你知道爲什麼可以嗎? – user2469122

+0

你有沒有添加使用? '使用System.Data;'或使用完全限定名稱:'var table = new System.Data.DataTable();' –

+0

我剛剛使用「using.system.data.oracleclient」。現在我說,這是公認的。但是現在我在連接字符串中有一個錯誤:「在ConnectionString中沒有指定OLE DB提供程序,例如'Provider = SQLOLEDB;'。」但我粘貼連接字符串,所以我認爲它是正確的:S – user2469122

0

我認爲你需要在你的連接字符串,以提供供應商名稱。