[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

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

0
dapple
Asked:
dapple
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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