Strange databinding problem with DataTextField and custom class

hi,
I have a custom class derived from collectionBase  as follows.

public class categoriesCollection : CollectionBase , IBindingList, IList
{




public int Add (categories value)
      {
            return List.Add(value);
      }
}
and so on .

Also a custom class that holds the data , which has public properties
public class categories : IComparable
{
private Int16              _id = 0;
      //keep a copy of the original so it can be used for editable primary keys.
      private Int16 _Originalid;
      private string
  _name = string.Empty;
      public string name
      {
            get { return _name; }
            set
            {
                  if (_name == value)
                        return;
                  _name = value;
                  _isdirty = true;
            }
      }
 }
I databind this to a drop down list , i can set the DataValueField , but anything i put in the DataTextField comes out blank. I understood that you have to have public propertys on the class so the databinding can get the values, which i have so i am at a loss why its blank. Is it a bug?

David

LVL 1
dappleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
Hi dapple,

have you tried:

dropdownlist1.DataTextField = categories.name ?

Regards!
B..M
mmarinov
0
CJ_SCommented:
Seems good to me. Are you sure the collection is filled? Can you show how you are binding to the dropdownlist?
0
dappleAuthor Commented:
no mmarinov , that doesnt work.
The collection is filled because the Value field gets set no problem, i have also looked in debug mode as well and their are values there. Databinding code:

ddlCategory.DataSource = categoriesDB.GetAll(connString);
ddlCategory.DataBind();

private static categoriesCollection GetAll(TransactionManager trans, string ConnectionString, int start, int pagelen)
      {
            //Declare Varibles
            SqlDataReader reader;
            if(trans != null)
                  reader = SqlHelper.ExecuteReader(trans.TransactionObject, "Categories_GetList");
            else
                  reader = SqlHelper.ExecuteReader(ConnectionString, "categories_GetList");
            //Create Collection
            categoriesCollection rows = new categoriesCollection();
            Fill(reader, rows, start, pagelen);
            reader.Close();
            return rows;
      }//end getall
protected static categoriesCollection Fill(SqlDataReader reader, categoriesCollection rows, int start, int pagelen)
      {
            int recordnum = 0;
            while (reader.Read() && (pagelen != 0))
            {
                  if(recordnum >= start)
                  {
                        categories c = new categories();
                        c.id = (Int16)reader["id"];
                        c.Originalid = (Int16)reader["id"];
                        c.name = (Convert.IsDBNull(reader["name"]))?string.Empty:(string)reader["name"];
                        c.AcceptChanges();
                        rows.Add(c);
                        pagelen -= 1;
                  }
                  recordnum += 1;
            }
            return rows;
      }
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

dappleAuthor Commented:
any ideas, another dead question? I have three open questions now i havent had solutions to.
0
CJ_SCommented:
I seem to have missed your comment with code.

Still, your code looks correct to me.

Did you do something like:

ddlCategory.DataSource = categoriesDB.GetAll(connString);
ddlCategory.DataValueField = "Originalid";
ddlCategory.DataTextField = "name";
ddlCategory.DataBind();

?
0
dappleAuthor Commented:
well i solved it by just directly using the reader rather than the collection.
im still not sure why it didnt work.

public static void GetAll( string ConnectionString  , System.Web.UI.WebControls.DropDownList ddl)
      {
                  GetAll(null,ConnectionString  , ddl) ;
      }
      
      public static void GetAll(TransactionManager trans,string ConnectionString  , System.Web.UI.WebControls.DropDownList ddl)
      {
            SqlDataReader reader;
            if(trans != null)
                  reader = SqlHelper.ExecuteReader(trans.TransactionObject, "Categories_GetList");
            else
                  reader = SqlHelper.ExecuteReader(ConnectionString, "Categories_GetList");
            ddl.DataSource = reader;
            ddl.DataBind();
            reader.Close();
      }
0
moduloCommented:
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.