Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
>I have 100 stored procedure which returns data which is uncertain (it could return 1 to n numbers of columns with single or multiple rows)
I am using Entity framework which returns me a class object or a list<> of the object.
What I needed is to create a single function in which I can pass the output of my any StoredProcedure
and regardless the structure of the data it generates an HTML (Which I return directly to UI) Also I need to access field name too.
So if my sp returns

var result1 = DB.getServerDetailsBackupDefincation(servername);
var result2 = DB.getServerDetailsStorageDevice(servername);
I want to send result in a fucntion which will accpet result either as dynamic or as object .

private static void loop(object result)
{
}
private static void loop(dynamic result){
}
And now I have to access all the item in the list in result so I can loop through it
and create a header with column name
And rows if they exist in the result.

So far I am able to get the column name.

PropertyInfo[] propertyInfo = obj.GetType().GetProperties();
Type type=propertyInfo[2].GetMethod.ReturnType; /to access list type
PropertyInfo[] propertyInfo1 = type.GetProperties();

foreach (PropertyInfo p in propertyInfo1)
{
Console.WriteLine(p.Name); //
}

I hope you understand what I need
Thanks

What I have tried:

private static void loop(object obj)
{
string xPrev = "";
PropertyInfo[] propertyInfo = obj.GetType().GetProperties();
Type type=propertyInfo[2].GetMethod.ReturnType;
PropertyInfo[] propertyInfo1 = type.GetProperties();



foreach (PropertyInfo p in propertyInfo1)
{
Console.WriteLine(p.Name);

string propertyName = string.Join(".", new string[] { xPrev, p.Name });
if (!propertyName.Contains("Parent"))
{
Type propertyType = p.PropertyType;
if (!propertyType.ToString().StartsWith("MyCms"))
{
Console.WriteLine(string.Join(".", new string[] { xPrev, p.Name }).TrimStart(new char[] { '.' }));
}
else
{
xPrev = string.Join(".", new string[] { xPrev, p.Name });
}
}
}
}
}


public class Emp
{
public int id { get; set; }
public string name { get; set; }
}
public class Student
{
public int studentID { get; set; }
public string studentName { get; set; }
}
static void Main(string[] args)
{

List<emp> emp1= new List<emp>() { new Emp() { id = 1, name = "AAA" }, new Emp() { id = 2, name = "BBB" } };
List<student> student1= new List<student>() { new Student() { studentID = 1, studentName = "S1" }, new Student() { studentID = 2, studentName = "S2" } };
loop(emp1);
//loop(student1);
Console.ReadLine();

}
Posted
Updated 8-Aug-16 20:57pm
v2
Comments
The Praveen Singh 9-Aug-16 0:47am    
where is the problem or what error you get ?
arry.net 9-Aug-16 1:47am    
In loop function {loop(object result)}
I can access the object it has 3 things in it
Capacity,Count,{DynamicListLoop.Emp Item [Int32]}
I am unable to loop through the this
Adding code in (What I have tried:) section so you can run it directly

1 solution

Found the Solution

C#
public static void GenerateHtml(object obj)
        {
            string htmlData = "<table class='table table-bordered table - responsive table - hover'>";
            StringBuilder sbHeader = new StringBuilder();
            StringBuilder sbBody = new StringBuilder();

            foreach (PropertyInfo pi in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(IList).IsAssignableFrom(pi.PropertyType))
                {
                    IList elms = (IList)pi.GetValue(obj, null);
                    if (elms != null)
                    {
                        sbHeader.Append(htmlData + "<thead><tr class='table-info'>");
                        foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(elms[0]))
                        {
                            sbHeader.Append("<th>" + descriptor.Name+"</th>");
                        }
                        sbHeader.Append("</tr></thead>");
                        sbBody.Append("<tbody>");
                        for (int i = 0; i < elms.Count; i++)
                        {
                            sbBody.Append("<tr class='table-active'>");
                            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(elms[i]))
                            {
                                //string name=descriptor.Name;
                                //object value=descriptor.GetValue(elms[i]);
                                //Console.WriteLine("{0}={1}", name, value);
                                sbBody.Append("<td>" + Convert.ToString(descriptor.GetValue(elms[i])) + "</td>");
                            }
                            sbBody.Append("</tr>");
                        }
                        sbBody.Append("<tbody></table>");
                    }
                }
                else
                {
                    Console.WriteLine(pi.Name + "=" + pi.GetValue(obj, null));
                }
            }
            Console.WriteLine(sbHeader.ToString() + sbBody.ToString());
        }
 
Share this answer
 

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