ComboBox issue

Last post 08-15-2008 9:53 PM by uriorlov. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-21-2008 6:02 PM

    • uriorlov
    • Top 50 Contributor
    • Joined on 12-03-2007
    • Wannabe Slacker
    • Points 570

    ComboBox issue

    Hello,

    I need to populate a ComboBox from a database and bind a field in a database to the ComboBox so you can select its corresponding value. How can I do this? Please help. 

    Digging deeper into .Net

    Uri Orlov
    Ukraine
  •  Advertisement

    Featured Advertisement

     
  • 07-01-2008 1:15 AM In reply to

    Re: ComboBox issue

    what I understand is; you need to bind the DropDownList with a DataBase Table and to get the Corresponding ID Value of the selected item in ComboBox.

    Below is one sample code for how to bind the DropDownList with DataBase table. Important Properties to set are > DataSorce, DataTextField, DataValueField and last one important method to call is DataBind() of DropDownList :

            SqlConnection sqlCN = new SqlConnection();
            sqlCN.ConnectionString = ConfigurationManager.AppSettings["DBConn"].ToString();
            string strQuery = "SELECT UserID, UserName FROM TBL_Users Order By UserName";
            sqlCN.Open();
            SqlDataAdapter daUsers = new SqlDataAdapter(strQuery, sqlCN);
            DataSet dsUsers = new DataSet();
            daUsers.Fill(dsUsers);
            if (daUsers != null && dsUsers.Tables.Count > 0 && dsUsers.Tables[0].Rows.count > 0)
            {
                ddlUserList.DataSource = dsSourceSite;
                ddlUserList.DataTextField = "SiteName";
                ddlUserList.DataValueField = "SiteID";
                ddlUserList.DataBind();
                ddlUserList.Items.Insert(0, new ListItem("--Select User--", "-1"));
            }
            if (sqlCN.State == ConnectionState.Open)
                sqlCN.Close();
            sqlCN.Dispose();

     

    To get the UserID of Selected User in the ComboBox in above example :

    you can get the UserID of selected User in SelectedIndexChanged event as,

    ddlUserList.SelectedItem.Value will give you UserID of selected User and

    ddlUserList.SelectedItem.Text will give you selected UserName

    let me know if you need further assistance..

    here is another detailed tutorial > DataBind DropDownList in ASP .Net

    and MSDN Link > DropDownList Class (System.Web.UI.WebControls)

    hope it helps./.

     

    "I would love to change the world, but they won’t give me the source code"

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point & mark your thread as Resolved for the sake of Future Readers.

    KaushaL || BLOG || Profile || Microsoft MVP
  • 08-15-2008 9:53 PM In reply to

    • uriorlov
    • Top 50 Contributor
    • Joined on 12-03-2007
    • Wannabe Slacker
    • Points 570

    Re: ComboBox issue

    Hi Kaushal,

    It's so nice of you helping me like this. Thanks a lot for your help and please forgive me for my late reply.

    Digging deeper into .Net

    Uri Orlov
    Ukraine
    Filed under: ,
Page 1 of 1 (3 items)