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<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