This may seem like a simple question but I for the life of me cannot seem to get this working.

I have an ASP page that I need to set a dropdown box to a value from sql.

I have the dropdown getting filled from sql no problem.

private void fillDMSType()

            string selectDMStype = "select distinct dms_type_alias, dms_type_id from dms_types";

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(selectDMStype, con);
            SqlDataReader reader;

                reader = cmd.ExecuteReader();
                while (reader.Read())
                    ListItem newItem = new ListItem();
                    newItem.Text = reader["dms_type_alias"].ToString();
                    newItem.Value = reader["dms_type_id"].ToString();
            catch (Exception err)

The problem is when the page loads I need to set the dropdown to a corresponding request from sql.

protected void getDealersDMSAccessValue(int dealerID)
            SqlConnection con = new SqlConnection(connectionString);
            DataTable tblDMS = new DataTable();

            string dmsInfo = "select * from dms_types where dealer_id = " + dealerID;

            SqlDataAdapter adapt = new SqlDataAdapter(dmsInfo, con);

            if (tblDMS.Rows.Count > 0)
                // = (int)tblDMS.Rows[0]["dms_access_method_id"];
                this.dmsdrpid.Text = (string)tblDMS.Rows[0]["dms_type_id"].ToString();
                //this.drpDMSType.SelectedValue = (string)tblDMS.Rows[0]["dms_type_id"].ToString();
                this.drpDMSType.SelectedItem = this.drpDMSType.Items.FindByText((string)tblDMS.Rows[0]["dms_type_alias"]);

I have searched high and low and cannot seem to figure the correct way to do this.

Again I am sure that it is pretty simple and I am just missing something. Any help would be greatly appreciated.

what you need is just this

this.drpDMSType.Items.FindByText((string)tblDMS.Rows[0]["dms_type_alias"]).Selected = true;

take care.

This article has been dead for over six months. Start a new discussion instead.