I have been working on a wcf application and have been trying to return a list of names from an sql server database. My main issue is that my select * statement only returns the last value of the list. Here is the code.
Name Model:
public Name GetName()
{
Name name = new Name();
using (SqlCommand objcmd = new SqlCommand("Select * from Names", sql))
{
SqlDataReader reader = objcmd.ExecuteReader();
while (reader.Read())
{
name.NameID = reader.GetInt32(0);
name.Name = reader["Name"].ToString();
}
}
return name;
}
.svc:
public Name GetNames()
{
Name serviceName = new Name();
NamesModel model = new NamesModel();
Name modelName = model.GetName();
serviceName.NameID = modelName.NameID;
serviceName.Name = modelName.Name;
model.Close();
return serviceName;
}
NameService:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "getnames")]
Name GetNames();
I have tried to print the value out to a list but there seems to be a disconnect somewhere. Any help would be great. Thanks.
Not sure, but the code shown definitevely doesn’t return any list….
I try to guess your intentions and suggest to change in this way ….
.svc:
NameService: