c# - Creating a Service To Return a List -
within service1.svc file have following;
[operationcontract] public list<accounts> getallaccounts() { string conn = "......"; list<accounts> accs = new list<accounts>(); sqlconnection _con = new sqlconnection(conn); sqlcommand sc = new sqlcommand("sql query....", _con); sqldatareader reader; _con.open(); reader = sc.executereader(); while (reader.read()) { accs.add(new accounts() { name = reader["name"].tostring(), qty = convert.toint32(reader["qty"]), weight = convert.todecimal(reader["weight"]), value = convert.todecimal(reader["value"]) }); } _con.close(); return accs.tolist(); } public class accounts { public string name { get; set; } public int qty { get; set; } public decimal weight { get; set; } public decimal value { get; set; } }
i know sql correct , have printed contents of list screen ensure data there, problem have when try run service in browser - fails.
now, believe because iservice.cs conflicting returned 'list' control. error is;
my service1.getallaccounts() method cannot implement iservice1.getallaccounts() because doesn't have matching return type of 'system.....list'.
iservice1.cs;
[servicecontract] public interface iservice1 { [operationcontract] list<accounts> getallaccounts(); } public class accounts { public string name { get; set; } public int qty { get; set; } public decimal weight { get; set; } public decimal value { get; set; } }
this issue sounds simple, i'm new c#, advise?
add [datacontract]
, [datamember]
attribute accounts class
[datacontract] public class accounts { [datamember] public string name { get; set; } [datamember] public int qty { get; set; } [datamember] public decimal weight { get; set; } [datamember] public decimal value { get; set; } }
also, client code uses service should configured receive collection type system.collection.generic.list
right click service reference , select "configure service reference". in collection type combo select system.collection.generic.list instead of default array
just completeness, others have said, makes no have 2 class identical names accounts. confuse compiler point receive error. , have created list variable so, return variable.
Comments
Post a Comment