Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

ListBox datasource works fine in Win Forms, but Broken in Web Forms

Hi all.

I have the following problem

I have a listbox which is bound to a datatable with two columns(about 25 rows).

In C# win forms, it works just like it says it should, but if i move the code to Web Forms, the listbox comes back empty. I have checked, and the table definitely has data.

I'm new to Web Forms though, so its entirely possible I'm missing something obvious.

Any hints?

-------------------------
Win Forms Code
-------------------------
      try {
        this.dirsearchGroups.SearchRoot = de;
        SearchResultCollection rs1 = this.dirsearchGroups.FindAll();
        dtGroups = new DataTable("groups");
        dtGroups.Columns.Add("group", System.Type.GetType("System.String"));
        dtGroups.Columns.Add("path", System.Type.GetType("System.String"));

        foreach(SearchResult rs in rs1) {
          dtGroups.Rows.Add(new object[2] {rs.Properties["cn"][0].ToString(), rs.Path.ToString()});
        }
        this.lbGroups.DataSource = dtGroups;
        this.lbGroups.DisplayMember = "group";
        this.lbGroups.ValueMember = "path";
snip--->

**WORKS LIKE A CHARM**

------------------------------
Web Forms
------------------------------
      dirsearchGroups.SearchRoot = root;
      dirsearchGroups.Filter = "(&(objectClass=group)(cn=*))";
      dirsearchGroups.PropertiesToLoad.Add("cn");

      SearchResultCollection rs1 = this.dirsearchGroups.FindAll();
      dtGroups = new DataTable("groups");
      dtGroups.Columns.Add("group", System.Type.GetType("System.String"));
      dtGroups.Columns.Add("path", System.Type.GetType("System.String"));


      foreach(SearchResult rs in rs1) {
        dtGroups.Rows.Add(new object[2] {rs.Properties["cn"][0].ToString(), rs.Path.ToString()});
      }
      Response.Write("dtGroups count " + dtGroups.Rows.Count.ToString());

      this.lbGroups.DataSource = dtGroups;
      this.lbGroups.DataTextField = "group";
      this.lbGroups.DataValueField = "path";

**THIS RETURNS AND EMPTY "ListBox" (select box on the HTML rendered form)

Can somebody clue me in please?  I'm sure its something I'm not doing..
0
SegFault
Asked:
SegFault
1 Solution
 
RiverGuyCommented:
I ran accross this too.  I ended up tossing out the data-binding and adding values to the listbox/combobox while looping through my dataset.

This  is in VB.Net, but the idea is the same in C#:

With list1
            Dim dr3 As Data.DataRow
            Dim k As Double
            k = 0
            For Each dr3 In DataSet2.Tables(0).Rows
                .Items.Add(dr3.Item(1))
                .Items.Item(k).Value = dr3.Item(0)
                k = k + 1
            Next
End With

I assigned the display value to the 2nd column in my DB, and the Value to the 1st column.
0
 
SegFaultAuthor Commented:
Here is how I did it

I'm using a DirectoryResults collection, so I just iterated through that collection and added the items.

foreach(SearchResult rs in rsCollection) {
  lbGroups.Items.Add(new ListItem(rs.Properties["cn"][0].ToString(), rs.Path.ToString());
}

worked like a charm.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now