Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Silverlight4
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--
[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 GetExcelRevenue(string flagcmb, string type, string cust_code, string cust_name)
{
 
List lstResult1 = new List();
 
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 27-Sep-12 23:34pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
n.podbielski at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 GetExcelRevenue(string flagcmb, string type, string cust_code, string cust_name)
{
 
List lstResult1 = new List();
 
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() 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..
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 CPallini 189
3 Sergey Alexandrovich Kryukov 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,958
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100