not adding correct value from asp dropdown to database

Populate dropdown
myConnection.Open();
            
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("Select ID, DispatcherName FROM Dispatchers", myConnection);
            myReader = myCommand.ExecuteReader();
            Dispatcher.Items.Clear();
            while (myReader.Read())
            {
                string DispatcherName = myReader["DispatcherName"].ToString();
                string ID = myReader["ID"].ToString();
                Dispatcher.Items.Add(new ListItem(DispatcherName, ID));
            }

Open in new window


Form submission (add button click event)
SqlCommand myInsert = new SqlCommand("INSERT INTO Users (Dispatcher) Values ('" +Dispatcher.SelectedItem.Value + "')", myConnection);
                myInsert.ExecuteNonQuery();
                GridView1.DataBind();

Open in new window


aspx code
Company<br />
                    <asp:DropDownList ID="Dispatcher" runat="server"></asp:DropDownList>

Open in new window


This does not bomb at all and writes the record to the database, but it always writes the value of the first option of the dropdown regardless of what I select.
LVL 9
BobHavertyComhAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

You need to populate the DropDownList  within the PostBack.

protected void Page_Load(object sender, EventArgs e)
{
            if (!IsPostBack)
            {
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("Select ID, DispatcherName FROM Dispatchers", myConnection);
                myReader = myCommand.ExecuteReader();
                Dispatcher.Items.Clear();
                while (myReader.Read())
                {
                    string DispatcherName = myReader["DispatcherName"].ToString();
                    string ID = myReader["ID"].ToString();
                    Dispatcher.Items.Add(new ListItem(DispatcherName, ID));
                }

            }

// rest of the code goes here
}

Open in new window


When you click on Button again Page_Load event occurs and the DropDownList Binds and sets the First item as Selected.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
Tah! Dah! Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.