string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];
//創建一個SqlConnection
string SQL_Select = "select id, ItemName from DDLItem order by id desc";
//構造一個SqlDataAdapter
SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select,
//開始讀取數據
Conn.Open();
DataSet dataSet = new DataSet();
myAdapter.Fill( dataSet,"Table1" );
Conn.Close();
//開始綁定DropDownList
//指定DropDownList使用的數據源
DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;
//指定DropDownList使用的表里的那些字段
DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段
DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
DropDownList1.DataBind();
con.Open();
SqlCommand cmd = new SqlCommand(strSql,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(new ListItem(dr["status"].ToString(), dr["status_Id"].ToString()));
}