Listbox SelectedIndex set to -1

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I populate an unbound ListBox on a WebForm with the following C# code.

SqlDataReader oDr = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
ListBoxTableName.DataBind();

But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableName_SelectedIndexChanged method fires, but the
SelectedIndex value is -1. What am I doing wrong?
 
No.

I fixed the problem by changing my code to read like this:

sqlConnectionTableList.Open();
SqlDataReader oDr = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
if (!IsPostBack)
{
ListBoxTableName.DataBind();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top