林上的方法努力使我所有的SQL Querys,然後數據綁定他們中繼器,DataLists等等......數據綁定動態控制的類型和名稱
protected void sqlQuery(Control controlName, String query) {
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
cmd.Connection = conn;
try {
cmd.CommandText = query;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
controlName.DataSource = dt;
controlName.DataBind();
} catch (Exception error) {
Label1.Text = "Error!<br/>" + error;
}
}
,那麼我會調用該方法與我的控制姓名,我想要數據綁定。 像:
sqlQuery(Repeater1, "SELECT * FROM someTable");
sqlQuery(DataList1, "SELECT * FROM someTable");
但現在dosent工作,因爲它不知道的控制類型時,我只是用控制..
因此,如何將我做到這一點?發生
dataSourceProp.SetValue(控制,DT):無重載方法「的SetValue」需要兩個參數 –
NULL傳遞給第三個參數 –
Wor ks就像一個魅力,如果你能解釋你在做什麼,那會很棒:)它可以工作,但我不明白爲什麼:) –