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.
This leads me to believe that the SelectedIndex is reset when the button is clicked, which I don’t understand.
Code of delete button:
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());
}
}
}
Any help is appreciated.
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:
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();
}
}
Two other functions:
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());
}
}
}
You are possibly populating the dropdown in the Page_Load function and you do not have a Page.IsPostBack check. Thus everytime the list is populated again. Change the population code to something like: