Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class:
C#
public class ADClient
       {
           public int InstanceID = 1;
           public string ClientName = "";
           public string HostIP = "";
           public string UserName = "";
           public string Password = "";
           public bool IsPasswordEncrypted = true;
           public string HostFilePath = "";
           public string LocationTrim = "";
           public string ImageVideoPath = "";
           public string DestinationPath = "";
           public string FileType = "";
           public string RenameAfterCopy = "";
           public string BinaryAscii = "";
           public string LogFile = "";
           public int NumberOfDaysToKeepLog = 0;
           public int ADTransactionDays = 0;
           public int Hours = 0;
           public bool ADCleanUp = true;
       }


All these are some settings.
Now I have another function that should populate values for these settings. The function is:
protected void PopulateDataFromDatabase(RegistrationInfoSettingsDB _regInfoSettingDb)
{
ADClient ad_Client = new ADClient();
string categoryName = "Advance Diagnostics";
string settingValue = null;

string settingName = null;

SqlDataReader reader = _regInfoSettingDb.GetRegistrationInfoSetting(categoryName, ad_Client.InstanceID);
while (reader.Read())
{
settingValue = reader.GetValue(1).ToString().Trim();

settingName = reader.GetValue(2).ToString().Trim();
}

}

I have to compare on the basis of setting name and assign value. But i don't know how to do a string comparison for custom collection.
Posted
Comments
johannesnestler 3-Jul-14 8:09am    
aha - what custom collection? What string comparison you want to execute? property names with data columns to map the values?
Anshumaan Chaturvedi 3-Jul-14 8:10am    
exactly, property names with data column value to map setting value.
johannesnestler 3-Jul-14 8:37am    
before messing arround with a possible "Hack" solution by just hardcoding known property and column names, or a more "generic" solution using reflection to get the property names and then comparing it with a collection of column names, I'd ask if you ever considered an OR-Mapper (EntityFramework etc.) or typed datasets instead of sqldatareader?
Anshumaan Chaturvedi 3-Jul-14 8:50am    
Sir, let me be honest, i don't know up to your "esteemed level", I am just a college grad in intern, and people instead should encourage for the efforts any fresher puts,not discourage them :) Thanks for your kind words
johannesnestler 3-Jul-14 8:58am    
Sorry anshumaan, I think something was "lost in Translation", I don't understand what you are trying to tell me... My intend was just to Point out better solution for this kind of (common) problem (I mean mapping of in-memory types representig your data. If this problem is just an isolated one in your application and you are not expecting too much future change of your data structures and classes, a "hardcoded" mapping is easy and will do the job - of course this would not be a very maintainable, reusable or "pretty" solution... (See OriginalGriffs recommendation)

1 solution

Don't.
You can do it - Reflection will get the the property names and datatypes - but it'll be slow and awkward, and not particularly obvious.
Instead, fill the properties directly using compile time names:
C#
DestinationPath = (string) reader["DestinationPath"];
It'll be clearer, it'll mean your code doesn't fail if someone adds a column or a "calculated property" later, and it allows the DB columns to move around independently of the class properties.
 
Share this answer
 
Comments
Anshumaan Chaturvedi 3-Jul-14 8:47am    
Could you elaborate? Please if you can..
Anshumaan Chaturvedi 3-Jul-14 8:54am    
Please elaborate.....if you can

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