I’d like to return a list through a web service that takes a string which updates the SQL that supplies the list. I’m not too familiar with building lists, but I already have a list in my class. Again, I’d like to return this list through the web service.
My current list in my table class:
static public List<Skills> GetSkillsList(string like)
{
List<Skills> thelist = new List<Skills>();
string sql = "SELECT * FROM Skills WHERE SkillName LIKE '%" + like + "%'";
SqlDataReader dr = DBUtil.FillDataReader(sql);
while (dr.Read())
{
Skills obj = new Skills();
obj.skillID = Convert.ToInt32(dr["skillID"].ToString());
obj.skillName = Convert.ToString(dr["skillName"].ToString());
obj.skillReviewed = Convert.ToBoolean(dr["skillReviewed"].ToString());
obj.skillActive = Convert.ToBoolean(dr["skillActive"].ToString());
thelist.Add(obj);
}
return thelist;
}
Dummy list in the web service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
public IList<string> GetSkills(string contains)
{
IList<string> output = new List<string>();
output.Add("Apple");
output.Add("Ajax");
output.Add("Alpha");
output.Add("Alphred");
return output;
}
}
I’d like to return the Name and the ID fields in the web service list.
I would think it would be something like:
[WebMethod]
public IList<string> SkillsList(string like)
{
IList<string> output = new List<string>();
output = Skills.GetSkillsList(like);
return output;
}
but I know this is incorrect.
Any help is appreciated!
Here’s how to do it in the WebMethod you provided. As suggested, a new class to hold the ID and Name would be best. Although you could do a delimited string if you have a requirement to use List.