0

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;
}
3
  • 1
    Have you tried to add ListBox1.DataBind(); after ListBox1.DataSource = list; ? I think that should do the trick Commented Jan 12, 2016 at 22:45
  • I have, it throws me to the catch statement but I don't know why. (There is a try/catch not shown) Commented Jan 12, 2016 at 23:06
  • whats the exception? also try to put List<string> list = new List<string>(); before the if... Commented Jan 12, 2016 at 23:10

1 Answer 1

1

You don't need to create a list to manipulate your data, Just use following and then bind it to your ListBox control. I have considered FilePath as column name which you will be retrieving from database.

if (!Page.IsPostBack)
 {
     DataTable oDataTable1 = loadUserImageNames();
     foreach (DataRow dr in oDataTable1.Rows)
     {
         string filePath = (dr.Field<string>("FilePath"));
         string finalFileName = (filePath.Substring(filePath.LastIndexOf('/') + 1));
         dr["FilePath"] = finalFileName;
         // Or you can use following
         // dr["FilePath"] = (filePath.Substring(filePath.LastIndexOf('/') + 1)); // In One Line 
     }
     ListBox1.DataSource = oDataTable1;
     ListBox1.DataTextField = "FilePath";
     ListBox1.DataBind();
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.