Column does not exist in datasouruce issue when binding SubSonic collection to dropdown

I have a table with a Primary Key with _ (underscore) in its name, for example: User_Id. When SubSonic generates DAL, it removes underscores. I now bind a collection of objects to the DropDownList like this:

private void LoadCbo()
{
    UserCollection users=(new UserCollection()).Load();
    User u=new User(){
        UserId=-1,
        Name="[Select]"};
   users.Insert(0,u);

   ddlUsers.DataSource=users;
   ddlUsers.DataValueField=User.Columns.UserId;
   ddlUsers.DataTextField=User.Columns.Name;
   ddUsers.DataBind();    
}

      

When run it tells me that the object does not contain a column named "User_Id".

PS: - using "UserId" works great. I just want to know if this is a bug in SubSonic (2.1) or am I doing something wrong?

0


a source to share


2 answers


In SubSonic 2.2, you can also do this:

ddlUsers.DataValueField = User.UserIdColumn.PropertyName;

      



This way you can avoid hardcoding column names in your code.

+1


a source


The collection of columns consists of the names of the columns in the database, not the names of the properties of the object. This is not a bug, which is an essential part of the functionality, otherwise SubSonic will not know how to query the actual database.

The next line indicates which property to use when populating the dropdown value:

ddlUsers.DataValueField=User.Columns.UserId;  

      



The User.Columns.UserId value will be "User_Id", which is the name of the column in your database table, not the name of the property. However, when ddlUsers binds data, it cannot find a property on the User object named User_Id because when SubSonic generates your DAL, it removes the underscore from the property name. Best fix (as pointed out in the work):

ddlUsers.DataValueField = User.UserIdColumn.PropertyName;  

      

+2


a source







All Articles