I'm having trouble populating a dropdownlist with listItems from an Array. I'm creating a search bar to search through items that I've retrieved from a database that I stored in an array. I gave it a shot, and below is what I've come up with.
Nothing happens when the button is clicked. No exceptions or errors.
Thanks in advance!
public partial class Client_Default : System.Web.UI.Page
{
List<string> clients = new List<string>();
List<string> id = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection cnn = new SqlConnection("Data Source=EDITOR1;Initial Catalog=info;Integrated Security=True"))
{
SqlDataAdapter da = new SqlDataAdapter("select name from Client", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Client");
SqlDataAdapter da2 = new SqlDataAdapter("select clientId from Client", cnn);
DataSet ds2 = new DataSet();
da.Fill(ds2, "Client");
foreach (DataRow row in ds.Tables["Client"].Rows)
{
clients.Add(row["name"].ToString());
}
foreach (DataRow row in ds2.Tables["Client"].Rows)
{
clients.Add(row["clientId"].ToString());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int x = 0; x > (clients.Count()-1); x++)
{
if (clients[x].StartsWith(TextBox1.Text))
{
ListItem a = new ListItem(clients[x], id[x]);
DropDownList1.Items.Add(a);
}
else
{
}
}
}
Problem I see is your loop
for (int x = 0; x > (clients.Count()-1); x++)
should be
for (int x = 0; x < clients.Count(); x++)
Further,
You shouldn't need to query the database twice and iterate through the result twice.