Question

I have one List<string> containing column names and corresponding datatype

Suppose my list is:

List<string> oList = { 
             "Name-Varchar", 
             "UserID-int", 
             "Address-Varchar", 
             "DBO-Date", 
             "Salary-Money" };

Using the above List want to create a dynamic list having properties Name,UserID,Address,DBO,Salary Then want to fill this list by DB table information .To use this list may need to create instance.

  1. On run time want to create dynamic list.
  2. Fill list by DB values.
  3. Declare this list globally so need to create instance of the list.

Is this possible ?

If it is possible then how should I go about it?

Please provide me some workable syntax .If you have any query please ask.

No correct solution

OTHER TIPS

How about a custom class for the list items, and then just add one for each row retrieved from the database?

public class Employee
{
    string Name { get; set; }
    string UserID { get; set; }
    string Address { get; set; }
    string DBO { get; set; }
    string Salary { get; set; }
}

Create your list

List<Employee> lstEmployees = new List<Employee>;

And then to add each item pop it in a loop (let's say it's in a DataTable):

foreach (DataRow row in dt)
{
    Employee e = new Employee();
    e.Name = row["name"];
    e.UserID = row["userid"];
    e.Address = row["address"];
    e.DBO = row["dbo"];
    e.Salary = row["salary"];
    lstEmployees.Add(e);
}

Not really sure what you want... but try this

List<Dictionary<string,object>> YourList = new List<Dictionary<string,object>>();

foreach (DataRow row in dt)
{
    Dictionary<string,object> e = new Dictionary<string,object>();
    e["name"] = row["name"];
    e["userid"] = row["userid"];
    e["address"] = row["address"];
    e["dbo"] = row["dbo"];
    e["salary"] = row["salary"];
    YourList.Add(e);
}

Although I don't think that this i a good idea!

Please describe your goal more clearly so a better answer can be given...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top