Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made use of dataservice in my silverlight project.

CItems.cs is my class file in which i have defined all the classes.

now in my dataservice i have method to extract data from database..now what i want is a class should get generated along with its properties based upon the dataset result.i.e dataset columns should get added as properties in the class.so that i dont have to create a class with bulk of properties which r not used everytime.

Hope my problem is understandable..

CRevenue is my class.As we can see below i have to create object of CRevenue class and then access its properties.i think my class looks ugly with lot of properties in it.so i have to generate properties within class only as per requirement..means if my dataset returns only 2columns lets say 'Flag' and 'Type' then my class should contain only these two properties--
C#
[DataContract]
    public class CRevenue
    {
        [DataMember]
        public string FLAG { get; set; }
        [DataMember]
        public string TYPE { get; set; }
    }

so that if next time if i change my database table i dont have to add or delete any properties within my class.


Here's my dataservice method:-

[OperationContract]
public List<crevenue> GetExcelRevenue(string flagcmb, string type, string cust_code, string cust_name)
{

List<crevenue> lstResult1 = new List<crevenue>();

CRevenue objRev;

SqlConnection Conn = new SqlConnection(Connection_String);
SqlCommand cmd;
SqlDataAdapter Adapter;
cmd = new SqlCommand("Select_RevenueChartTable", Conn);
cmd.Parameters.AddWithValue("@flagcmb1", flagcmb);
cmd.Parameters.AddWithValue("@type1", type);
cmd.Parameters.AddWithValue("@cust_code1", cust_code);
cmd.Parameters.AddWithValue("@cust_name1", cust_name);

cmd.CommandType = CommandType.StoredProcedure;

Adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
Adapter.Fill(ds);

if (ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
objRev = new CRevenue();
if (!Convert.IsDBNull(dr["FLAG"]))
objRev.FLAG = dr["FLAG"].ToString();

if (!Convert.IsDBNull(dr["TYPE"]))
objRev.TYPE = dr["TYPE"].ToString();


if (!Convert.IsDBNull(dr["CUST_CODE"]))
objRev.CUST_CODE = dr["CUST_CODE"].ToString();

if (!Convert.IsDBNull(dr["CUST_NAME"]))
objRev.CUST_NAME = dr["CUST_NAME"].ToString();

if (!Convert.IsDBNull(dr["TOTAL"]))
objRev.TOTAL = Convert.ToDecimal(dr["TOTAL"]);


if (!Convert.IsDBNull(dr["PDC"]))
objRev.PDC = Convert.ToDecimal(dr["PDC"]);

if (!Convert.IsDBNull(dr["ONE_MTH"]))
objRev.ONE_MTH = Convert.ToDecimal(dr["ONE_MTH"]);

if (!Convert.IsDBNull(dr["TWO_MTH"]))
objRev.TWO_MTH = Convert.ToDecimal(dr["TWO_MTH"]);

if (!Convert.IsDBNull(dr["THREE_MTH"]))
objRev.THREE_MTH = Convert.ToDecimal(dr["THREE_MTH"]);

if (!Convert.IsDBNull(dr["FOUR_MTH"]))
objRev.FOUR_MTH = Convert.ToDecimal(dr["FOUR_MTH"]);

if (!Convert.IsDBNull(dr["FIVE_MTH"]))
objRev.FIVE_MTH = Convert.ToDecimal(dr["FIVE_MTH"]);

if (!Convert.IsDBNull(dr["SIX_MTH"]))
objRev.SIX_MTH = Convert.ToDecimal(dr["SIX_MTH"]);

if (!Convert.IsDBNull(dr["ONE_YR"]))
objRev.ONE_YR = Convert.ToDecimal(dr["ONE_YR"]);

if (!Convert.IsDBNull(dr["TWO_YR"]))
objRev.TWO_YR = Convert.ToDecimal(dr["TWO_YR"]);

if (!Convert.IsDBNull(dr["THREE_YR"]))
objRev.THREE_YR = Convert.ToDecimal(dr["THREE_YR"]);

if (!Convert.IsDBNull(dr["FOUR_YR"]))
objRev.FOUR_YR = Convert.ToDecimal(dr["FOUR_YR"]);

if (!Convert.IsDBNull(dr["FIVE_YR"]))
objRev.FIVE_YR = Convert.ToDecimal(dr["FIVE_YR"]);

if (!Convert.IsDBNull(dr["ABOVE_YR"]))
objRev.ABOVE_YR = Convert.ToDecimal(dr["ABOVE_YR"]);

lstResult1.Add(objRev);
}
}
return lstResult1;
}
hope my problem might b clear now
Posted

Hi,
I think You have to create a dynamic class or on run time. so you can use Dynamic object to create class and its members on run time. use dynamic object. and try it. you can find dynamic concept in .net framework 4.0.

Regards Rahul
 
Share this answer
 
Comments
n.podbielski 28-Sep-12 5:59am    
I think that in this case better will be deriving CRevenue from ExpandoObject.
Check http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx
hey hi! i used the dynamic object concept..it solved a bit my problem but now i m still facing problem..here's how i changed my dataservice method--

[OperationContract]
public List<expandoobject> GetExcelRevenue(string flagcmb, string type, string cust_code, string cust_name)
{

List<expandoobject> lstResult1 = new List<expandoobject>();

dynamic dynrevenue = new ExpandoObject();

SqlConnection Conn = new SqlConnection(Connection_String);
SqlCommand cmd;
SqlDataAdapter Adapter;
cmd = new SqlCommand("Select_RevenueChartTable", Conn);
cmd.Parameters.AddWithValue("@flagcmb1", flagcmb);
cmd.Parameters.AddWithValue("@type1", type);
cmd.Parameters.AddWithValue("@cust_code1", cust_code);
cmd.Parameters.AddWithValue("@cust_name1", cust_name);

cmd.CommandType = CommandType.StoredProcedure;

Adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
Adapter.Fill(ds);

DataTable dt = new DataTable();
dt = ds.Tables[0];

string[] columnname = (from colnam in dt.Columns.Cast<datacolumn>() select colnam.ColumnName).ToArray();

if (ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{

foreach (string colname in columnname)
{

if (!Convert.IsDBNull(dr[colname]))
dynrevenue.something = (dr[colname]).ToString();
}
lstResult1.Add(dynrevenue);
}
return lstResult1;
}


now what i m facing is.. inside the foreach loop i can use the iterator 'colname' which contains the column name of the dataset. i.e dr[colname] but how can i change the left side property for every iteration? i mean 'dynrevenue.something' gets overwritten after each iteration.

Hope i m going right..
 
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