Note that there are some explanatory texts on larger screens.

plurals
  1. PONot getting selected value from drop down box
    primarykey
    data
    text
    <p>I'm having an issue getting the correct value from my drop down box. Basically what I have is a program with a drop down list that is populated with names that are saved in a database table. The user selects a name from the drop down box and then clicks a button to remove the name from the database. The issue I am having is that no matter which name is selected, the first name (with an index of 0) is deleted from the drop down box.</p> <p>This leads me to believe that the SelectedIndex is reset when the button is clicked, which I don't understand.</p> <p>Code of delete button:</p> <pre><code>protected void cmdDelete_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;")) { try { conn.Open(); SqlCommand comm = new SqlCommand("DELETE FROM Names WHERE FirstName=@FirstName AND LastName=@LastName", conn); char[] delims = new char[1]; delims[0] = ' '; //lblGreeting.Text = cblNames. lblGreeting.Text = ddlNames.SelectedItem.Text; string[] names = ddlNames.SelectedValue.ToString().Split(delims); string fname = names[0]; string lname = names[1]; comm.Parameters.Add(new SqlParameter("@FirstName", fname)); comm.Parameters.Add(new SqlParameter("@LastName", lname)); comm.ExecuteNonQuery(); conn.Close(); LoadTable(); } catch (Exception ex) { Response.Write("Delete Table: "+ex.ToString()); } } } </code></pre> <p>Any help is appreciated.</p> <p>I'm not really sure what should change if the page is not being rendered for the first time or why that would change what I want that function to do. It is being called after the button is clicked, and the value should be taken before the table is rendered again. I'll include LoadTable() in case it helps:</p> <pre><code>protected void LoadTable() { using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;")) { try { conn.Open(); using (SqlCommand comm = new SqlCommand("SELECT * FROM Names",conn)) { SqlDataReader rd = comm.ExecuteReader(); string fname=""; string lname=""; ddlNames.Items.Clear(); while (rd.Read()) { fname = rd.GetString(0); lname = rd.GetString(1); ddlNames.Items.Add(fname + " " + lname); } rd.Close(); } } catch (Exception ex) { Response.Write("Load Table: "+ex.ToString()); } conn.Close(); } } </code></pre> <p>Two other functions:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { LoadTable(); } protected void cmdAdd_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;")) { try { conn.Open(); char[] splits = new char[1]; splits[0] = ' '; string fname = txtName.Text.Split(splits)[0]; string lname = txtName.Text.Split(splits)[1]; SqlCommand comm = new SqlCommand("INSERT INTO Names VALUES(@FirstName,@LastName)",conn); comm.Parameters.Add("@FirstName", fname); comm.Parameters.Add("@LastName", lname); comm.ExecuteNonQuery(); conn.Close(); LoadTable(); } catch(Exception ex) { Response.Write("Add Name: "+ex.ToString()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload