Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
//In my class I have private variables and properties like this.

C#
private string _itemCOde=string.Empty;
private string  _itemName=string.Empty;

public string ItemCode
        {
            get { return _itemCode; }
            set { _itemCode = value == null ? value : value.Trim();}
        }
public string ItemName
        {
            get { return _itemName; }
            set { _itemName = value == null ? value : value.Trim();}
        }


According to this properties I create Item objects after selecting the data from the sql table.
Now, if database table altered and add a new column called cost, then I have to add another property to the class. Without doing this is there any way to add property according to the database table.
Posted
Updated 19-May-11 23:34pm
v2
Comments
Fredrik Bornander 20-May-11 5:34am    
Added code blocks.

Google reveals all. I searched for "c# add properties dynamically", and this was one of the first results returned:

http://www.eggheadcafe.com/community/aspnet/2/10070284/how-to-dynamically-add-propery-in-a-class-in-c.aspx[^]
 
Share this answer
 
There are many different ways of achieving this, from approaches such as turning your object into a IDictionary based property bag, to using an ORM mapper[^].

Or just type it by hand, a well designed DB schema should not experience too many changes to its table definitions anyway.

/Fredrik
 
Share this answer
 
Comments
Sanyon_d 23-May-11 0:37am    
Thanks for the reply. In my form I have grid to show the contents of the table. For that grid user can add columns as they need and can add values to the field. When user adding (string,numeric etc) field to the grid, I want that to be add as a column in the table and need to save that value in correct column. After that when user retriewing data I want to populate properties for the object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900