During website development, we will use DropDownList to bind data. After we bind data, we want to display the Name, but after selection, the value is Id, we can use DataSet to read and bind data.
If (! Page. isPostBack) {// link to the database string connStr = ConfigurationManager. deleetask[ "Sunny"]; // create a SqlConnection conn = new SqlConnection (connStr); // The data to be queried string SQL = "select id, name from Caid "; // construct a SqlDataAdapter myAdapter = new SqlDataAdapter (SQL, conn); DataSet dataset = new DataSet (); try {// start to read the data source conn. open (); myAdapter. fill (dataset, "CaidTable");} catch (Exception) {throw;} finally {conn. close ();} // start to bind the data source. // specify the data source drpCaid used by DropDownList. dataSource = dataset. tables ["CaidTable"]. defaultView; // specify the fields in the DropDownList table. dataTextField = "name"; drpCaid. dataValueField = "id"; // bind data to drpCaid. dataBind (); drpCaid. autoPostBack = true ;}