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