我想用數據庫中的數據填充GridView並讓用戶選擇顯示「Cursussen」表的哪一部分。他可以通過更改下拉框中參數的選擇來完成此操作。將參數添加到後面代碼中的Select查詢
錯誤: 方法「添加」沒有重載需要2個參數。
protected void Page_Load(object sender, EventArgs e)
{
ConnectionStringSettings planner =
ConfigurationManager.ConnectionStrings["CursusDatabase"];
DbConnection connection = new SqlConnection(planner.ConnectionString);
DbCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"SELECT * FROM Cursussen " +
"WHERE CursusId = @CursusId";
cmd.Parameters.Add("CursusId", DropDownList1.SelectedValue); // <-- here
connection.Open();
DbDataReader rdr = cmd.ExecuteReader();
DataTable planning = new DataTable();
planning.Load(rdr);
connection.Close();
GridView1.DataSource = planning;
GridView1.DataBind();
}
}
哪個不行,因爲OP使用'DbConnection'和'DbCommand'而不是'SqlConnection'和'SqlCommand'。 – Oded
@Oded,我錯過了,謝謝指出 – Habib
我應該使用Sqlconnection和命令而不是dbconnection嗎? – Dave