Has anyone had any problems with the SelectedValue property not functioning properly within the ListBox? If so, how to fix it?
Basically, I am binding a list of values retrieved from a select statement in MySQL:
And I am trying to use fileList.Select edValue to retrieve the item which the user has selected before posting this value to another site, which the user is automatically redirected to when they click on a button.
As mentioned, no item is selected from the list box.
I also tried using SelectedItem, suggested by GaryTexmo, as well as using SelectedItem.Va lue, declaring a new ListBox in the Button2_Click method with the same name and hard-coding values into the ListBox but none have retrieved the value which I select from the list box
I'm sure I've used this property in a listBox before but somehow, it seems to give me an 'object reference is not set to an instance of an object error'.
Any ideas for how to solve what should be, a simple problem?
Regards
Matt
Basically, I am binding a list of values retrieved from a select statement in MySQL:
Code:
/*
* Display a list of all the papers stored within the user's database
*/
protected void Page_Load(object sender, EventArgs e)
{
using (MySqlConnection sqlCon = new MySqlConnection(ConnectionString))
{
sqlCon.Open();
MySqlCommand command = sqlCon.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = @"SELECT Title FROM Paper";
List<String> titles = new List<String>();
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (reader[0].ToString() != string.Empty)
{
titles.Add(reader[0].ToString());
}
}
}
fileList.DataSource = titles;
fileList.DataBind();
}
}
Code:
protected void Button2_Click(object sender, EventArgs e)
{
string file = fileList.SelectedItem.Text;
Response.Redirect(string.Format("EditRecordDetails.aspx?Title={0}", file));
}
I also tried using SelectedItem, suggested by GaryTexmo, as well as using SelectedItem.Va lue, declaring a new ListBox in the Button2_Click method with the same name and hard-coding values into the ListBox but none have retrieved the value which I select from the list box
I'm sure I've used this property in a listBox before but somehow, it seems to give me an 'object reference is not set to an instance of an object error'.
Any ideas for how to solve what should be, a simple problem?
Regards
Matt
Comment