1
votes

I have a dropdownbox whenever the value changes in it, the textbox should get updated by executing the select query in the following method. But what happens is the query always returns the result of the first item in the dropdown box. When i debug the program the value of the text is always the value of the first item in the dropdown box.

If I use SELECT suburb FROM sites WHERE SiteName = '" + siteId.SelectedValue + "'" Then I get correct values displayed in the textbox. When the query is Select address from sites thats when i am having trouble get the correct value.

protected void siteId_SelectedIndexChanged(object sender, EventArgs e) { string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|db.mdb"; using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connect)) {

         conn.Open();
        string insertQuery= "SELECT SiteAddress FROM sites WHERE SiteName = '" + siteId.SelectedValue + "'";
        using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(insertQuery, conn))
        {

             object text =cmd.ExecuteScalar();
             string final = text.ToString();

             TextBox1.Text = final;

            conn.Close();
            Dispose();            }
    }
   }
2

2 Answers

0
votes

My guess, go to where you have your list populated and add if(!Page.IsPostBack)

if(!Page.IsPostBack)
{
    //bind items to dropdown list here
}
0
votes

You need to clean up the existing values (Old vlaues) in the dropdown before adding changed values (new values) to the dropdown.