By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

Load Data from Database as Class Properties or enum

P: n/a
Hi,

I have a table with the data like this,

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't know
how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am wrong.

Any ideas how to go about it?

Regards,
Waqas Pitafi
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Try using ObjectSpaces due in Whidbey and ADO.NET 2.0 providing object to
database mapping which is what i beleive you are looking for.

HTH
rawCoder
"Waqas Pitafi" <Wa*********@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
Hi,

I have a table with the data like this,

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't know how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am wrong.
Any ideas how to go about it?

Regards,
Waqas Pitafi

Nov 21 '05 #2

P: n/a
I am sorry but I need the solution using .NET 1.1.

"rawCoder" wrote:
Try using ObjectSpaces due in Whidbey and ADO.NET 2.0 providing object to
database mapping which is what i beleive you are looking for.

HTH
rawCoder
"Waqas Pitafi" <Wa*********@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
Hi,

I have a table with the data like this,

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't

know
how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am

wrong.

Any ideas how to go about it?

Regards,
Waqas Pitafi


Nov 21 '05 #3

P: n/a
Waqas,

You say you want to use this enum throughout your project however for what.

What is not Unread, what is Deleted, what is Read.

After that it is maybe to tell how you can do that.

Cor

ID Status Description
1 R Read
2 U UnRead
D D Deleted

I want to load this data in a class (say named Status) which I want to use
through out my project and should be able to access it like,

Status.Read or
Status.UnRead or
Status.Deleted

with each returning their corresponding values as in the database table
shown above.

To sum it up I want to create an enum with values loaded from database. I
think it may be possible using Reflection or Custom Attributes but don't know how to do it.

One more thing is if I use reflection than I guess I won't be able to use
Status.Read in design time which means no use of it, correct me if I am wrong.
Any ideas how to go about it?

Regards,
Waqas Pitafi

Nov 21 '05 #4

P: n/a
Waqas,

The answer is simple and again starting with a question, why do you have
that status in so many places as I understand while it is always the same.

Just set it in one place and use it.
In a module or in a property from a shared class.

When you do not know how to do the last, reply.

I hope this helps?

Cor
Nov 21 '05 #5

P: n/a
I guess I am not able to explain it properly. I will try it again,

see,

Generally speaking these Status values should be fixed then I can hardcode
them in my code but that's not the case. If a new Status is included in the
system, I have to add necessary code for that.

Now why do I have to change the code? See the following code sample.

// New questions
DataListBind(ref dlNew, new
QuestionsSystem(UserName).CategorySummary(Question sStatus.Unread));

// Waiting For Answer
DataListBind(ref dlRead, new
QuestionsSystem(UserName).CategorySummary(Question sStatus.Read));

// Archive
DataListBind(ref dlArchive, new
QuestionsSystem(UserName).CategorySummary(Question sStatus.Answered));
While this is my QuestionsStatus enum
public enum QuestionsStatus
{
Ignore = -1,
Unread = 1,
Read = 2,
ForwardedToConsultant = 3,
ForwardedToMe = 4,
Answered = 5,
MarkedForDeletion = 6
}

which I have created as a duplicate for my table data.

Now say we add a new Status in the table called 'NeverDeleteMe', I have to
return to this enum and add the new Status and recompile my code.

Hope now you get my problem.
Nov 21 '05 #6

P: n/a
You could store the status values in a XML config file and then write a
routine to update the XML file whenever a new status value is added.

Hope this helps.
"Waqas Pitafi" <Wa*********@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I guess I am not able to explain it properly. I will try it again,

see,

Generally speaking these Status values should be fixed then I can hardcode
them in my code but that's not the case. If a new Status is included in the system, I have to add necessary code for that.

Now why do I have to change the code? See the following code sample.

// New questions
DataListBind(ref dlNew, new
QuestionsSystem(UserName).CategorySummary(Question sStatus.Unread));

// Waiting For Answer
DataListBind(ref dlRead, new
QuestionsSystem(UserName).CategorySummary(Question sStatus.Read));

// Archive
DataListBind(ref dlArchive, new
QuestionsSystem(UserName).CategorySummary(Question sStatus.Answered));
While this is my QuestionsStatus enum
public enum QuestionsStatus
{
Ignore = -1,
Unread = 1,
Read = 2,
ForwardedToConsultant = 3,
ForwardedToMe = 4,
Answered = 5,
MarkedForDeletion = 6
}

which I have created as a duplicate for my table data.

Now say we add a new Status in the table called 'NeverDeleteMe', I have to
return to this enum and add the new Status and recompile my code.

Hope now you get my problem.

Nov 21 '05 #7

P: n/a
Waqas,

You show exactly what I tried to say in my previous question. And I try to
show you, that you are probably making it yourself very difficult.

QuestionStatuscurrent = 1
Or when you want to use the enum in that case
QuestionStatuscurrent = QuestionStatus.read

// New questions
DataListBind(ref dlNew, new
QuestionsSystem(UserName).CategorySummary(Question sStatuscurrent));

// Waiting For Answer
DataListBind(ref dlRead, new
QuestionsSystem(UserName).CategorySummary(Question sStatuscurent));

// Archive
DataListBind(ref dlArchive, new
QuestionsSystem(UserName).CategorySummary(Question sStatuscurrent));

And when you have this in more programs, make a shared variable from
QuestionStatusCurrent either using a module or nice with a shared class and
a property.

Cor

..
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.