Binding Data to DropDownList with ID hidden

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suganya
    New Member
    • Dec 2006
    • 39

    Binding Data to DropDownList with ID hidden

    Hi

    I have "Specialty" table with 2 fields (SPID,Specialty ).
    To bind data from "Specialty: table to the dropdownlist during page load, I have given the following coding

    protected void Page_Load(objec t sender, EventArgs e)
    {
    if (!Page.IsPostBa ck)
    {
    SqlConnection con = new SqlConnection(" user id=sa;password= cast;database=H ello_Dr;server= AURORA-SERVER;");
    con.Open();
    String sql = "select * from Specialty";
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataReader dr1;
    dr1 = cmd.ExecuteRead er();
    //Speciality_Drop DownList.Items. Clear();
    while (dr1.Read())
    {
    Specialty_DropD ownList.Items.A dd(dr1.GetValue (1).ToString()) ;
    }
    }}

    But how can I bind both the column in the table by making 1 column hidden. That is making the specialty column binding based on specialty id.
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Load the data returned from your query into a datatable or dataset and use that collection as the datasource for your dropdownlist. You can set the DataTextValue and DataTextField properties of your dropdownlist = to the columns values in your collection.

    Code:
     ddl.DataTextValue = "SPID"; 
    ddl.DataTextField = "Specialty";
    ddl.DataSource = dataSet;
    ddl.DataBind();
    Nathan

    Comment

    Working...