473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamic Class Properties from Data Row Columns

I can't seem to locate any easy way to create dynamic class properties.
Essentially, I want to take the column headers from a data result set,
create a property having the column header name, and then set the value
of the property to the data in the corresponding field.

For example, if I had this row from table "users":

ID Name Age

1 Barry 35
2 Brenda 33

....I could then write a SQL statement, e.g., "SELECT * from users where
ID = 2"

....and then pass it through a class

Users myuser = new Users();

that would loop through the column names, creating the properties Name
and Age (right there!) and then setting the property value by looping
through the values. Accessing the "Users" class would yield...

Users.Name = Brenda
Users.Age = 33

What I'm trying to avoid is typing out 100 column header names, and then
having to add or subtract more names as the database changes...also, I
could create classes out of any-ol'-table much easier.

I'd sure appreciate some help!

--Brent
Nov 17 '05 #1
1 2613
You could in .NET but it would be way too slow at runtime.
Check out the source code for the object mapper below.
It creates the classes for you at design time. A much faster
approach.

--
Robbe Morris - 2004/2005 Microsoft MVP C#
Free Source Code for ADO.NET Object Mapper To DataBase Tables And Stored
Procedures
http://www.eggheadcafe.com/articles/..._generator.asp


"Brent" <"writeBrent at the google mail place"> wrote in message
news:11*************@corp.supernews.com...
I can't seem to locate any easy way to create dynamic class properties.
Essentially, I want to take the column headers from a data result set,
create a property having the column header name, and then set the value of
the property to the data in the corresponding field.

For example, if I had this row from table "users":

ID Name Age

1 Barry 35
2 Brenda 33

...I could then write a SQL statement, e.g., "SELECT * from users where ID
= 2"

...and then pass it through a class

Users myuser = new Users();

that would loop through the column names, creating the properties Name and
Age (right there!) and then setting the property value by looping through
the values. Accessing the "Users" class would yield...

Users.Name = Brenda
Users.Age = 33

What I'm trying to avoid is typing out 100 column header names, and then
having to add or subtract more names as the database changes...also, I
could create classes out of any-ol'-table much easier.

I'd sure appreciate some help!

--Brent

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rob | last post by:
Dear All, I have a class that exposes a few properties. These properties are used in a DataGridView. The code for that is shown bellow. The problem is that I also need to show...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
1
by: J.B | last post by:
I have a datagrid that will display different datasets, (it runs different sprocs based on a value in the querystring) but it will always return 6 columns. The 5th and 6th columns are always Date...
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
3
by: Lloyd Sheen | last post by:
Is there an example of VB code which will take a datasource and extract from that the needed info (columns etc.) to create the columns and then bind the data? This seems so much more difficult...
4
by: Bongard | last post by:
I have a dynamic range that I would like to use as a linked table into Access. The problem is that Access doesn't seem to want to to recognize the dynamic range when you click on "show named...
0
by: John Wright | last post by:
I have a class that I populate properties from a table in my database. I create the properties in my class one for one with the columns in the tables that I use to fill the class. Some properties...
2
by: DaveL | last post by:
Hello I want to build Dynamic Paramers for a Sql Insert below is what i have so far but...determinthe column type and length im having Problems with Tks dave string sInsert = "Insert into...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.