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()
        {
            this.drpDMSType.Items.Clear();

            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;

            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ListItem newItem = new ListItem();
                    newItem.Text = reader["dms_type_alias"].ToString();
                    newItem.Value = reader["dms_type_id"].ToString();
                    drpDMSType.Items.Add(newItem);
                }
                reader.Close();
            }
            catch (Exception err)
            {
            }
            finally
            {
                con.Close();
            }
        }

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);

            adapt.Fill(tblDMS);
            if (tblDMS.Rows.Count > 0)
            {
                //this.drpDMSType.SelectedIndex.Equals(tblDMS.Rows[0]["dms_type_id"]);
                // = (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.

hi,
what you need is just this

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

take care.

Yellowdog428,

To select an item from the list programatically, use selectedvalue property.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.