I'm missing something very simple here...I've stepped through my debugger and everything is working, but the ListBox is not being populated with the new data. It is showing as empty when I run the site.
I want to:
Step.1 Populate the Listbox with filepath strings from my database. (This is ok)
Step.2 Create a new List using the ListBox items, removing the path from each string in the process. (This seems to be ok, count is increasing for list when I debug)
Step.3 Repopulate the ListBox using my new 'list'. (Not Working)
<div>
<asp:ListBox ID="ListBox1" runat="server" Width="100%" Height="100%" AutoPostBack="true"/>
</div>
Retrieve the file paths from database and populate DataTable: (This is ok)
private DataTable loadUserImageNames()
{
string cpUsersConnection1 = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
SqlConnection oSqlConnection1 = new SqlConnection(cpUsersConnection1);
oSqlConnection1.Open();
SqlCommand oSqlCommand1 = new SqlCommand();
oSqlCommand1.Connection = oSqlConnection1;
oSqlCommand1.CommandType = CommandType.Text;
oSqlCommand1.CommandText = "SELECT FilePath from User_Images where AcNo ='" + AcNo.Text + "' ORDER BY AcNo";
SqlDataAdapter oSqlDataAdapter1 = new SqlDataAdapter();
oSqlDataAdapter1.SelectCommand = oSqlCommand1;
DataTable oDataTable1 = new DataTable("User_Images");
oSqlDataAdapter1.Fill(oDataTable1);
return oDataTable1;
}
Assign DataTable to Listbox (This is ok).
Recall each item from ListBox and remove Path, leaving only finalFileName (This is ok).
Assign finalFileName to list (I think this is ok, 'count' is increasing in debugging)
Assign list to ListBox1....this is not working, ListBox1 is empty.
if (!Page.IsPostBack)
{
DataTable oDataTable1 = loadUserImageNames();
ListBox1.DataSource = oDataTable1;
ListBox1.DataTextField = "FilePath";
ListBox1.DataBind();
List<string> list = new List<string>();
foreach (ListItem s in ListBox1.Items)
{
string filePath = (s.ToString());
string finalFileName = (filePath.Substring(filePath.LastIndexOf('/') + 1));
list.Add(finalFileName);
}
ListBox1.DataSource = list;
}