Creating a Service To Return a List
Within my Service1.svc file I have the 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 that my SQL is correct and I have printed the contents of this List
to screen to ensure the data is there, the problem I have however is when
I try to run the service in my browser - it fails.
Now, I believe this is because my IService.cs is conflicting with the
returned 'List' control. The error I get is;
My Service1.GetAllAccounts() method cannot implement my
IService1.GetAllAccounts() because it doesn't have the 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 very simple, but I'm fairly new to C#, could anyone advise?
No comments:
Post a Comment