Chris Turner Chris Turner - 1 month ago 15
ASP.NET (C#) Question

WCF Service populate data to ASP.net dropdownlist (Learning WCF Service)

I am attempting to learn the WCF Service to incorporate eventually incorporate with one of our websites.

I have created a test table in my database called, [TestTable]
I have been able to successfully write data to the table from a test site created in visual studios.

I am now attempting to populate a dropdownlist from the database. In this case, a list of all states.

There appears to be something I am missing. I can invoke the service and it will show all states in the WCF Test Client; however, I cannot seem to figure out how to attach the list it returns to the ASP.net dropdown list.

Here is my IService1.cs

[ServiceContract]
public interface IService1
{

[OperationContract]
//string GetData(int value);
string InsertApplicantDetails(ApplicantDetails appInfo);

[OperationContract]
List<US_States> GetAllStates();

//CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here

}


Here is my Service1.svc.cs for GetAllStates

public List<US_States> GetAllStates()
{
using (JobSeekersEntities db = new JobSeekersEntities())
{
var q = (from us_states in db.US_States
orderby us_states.abbr ascending
select us_states);

List<US_States> list_States = q.ToList<US_States>();

return list_States.ToList();

}
}


In the test website I have created the dropdownlist called, "dropdownStates".

I have added the ServiceReference and it is working as I can submit values to the database.
I have also created a Load() event for the dropdownlist.

Here is my Test page.

public partial class TestSubmission : System.Web.UI.Page


{
ServiceReference1.Service1Client objCon = new ServiceReference1.Service1Client();

protected void Page_Load(object sender, EventArgs e)
{

}

protected void dropdownStates_Load(object sender, EventArgs e)
{
//ServiceReference1.Service1Client myCon = new ServiceReference1.Service1Client();
dropdownStates.Items = ????

}


}

Any assistance would be greatly appreciated. Thank you for your time.

Answer

First make sure you have defined proper DataContract and Service Contract. In following sample I have defined US States with 2 properties. Pay attention to [OperationContract] and [DataContract] declarations!

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        List<US_States> GetAllStates();

    }


    [DataContract]
    public class US_States
    {
        [DataMember]
        public int StateId { get; set; }

        [DataMember]
        public string StateName { get; set; }
    }
}

Next add WebService reference to your project amd make a call like this: (Binding StateName to Text and StateId to Value)

    protected void dropdownStates_Load(object sender, EventArgs e)
    {
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        dropdownStates.Items.Clear();
        dropdownStates.DataSource = client.GetAllStates();
        dropdownStates.DataTextField = "StateName";
        dropdownStates.DataValueField = "StateId";
        dropdownStates.DataBind();
        client.Close();
    }

And finally the Service:

namespace WcfService1
{
    public class Service1 : IService1
    {
        public List<US_States> GetAllStates()
        {
            List<US_States> result = new List<US_States>();
            result.Add(new US_States() { StateId = 1, StateName = "New York" });
            result.Add(new US_States() { StateId = 2, StateName = "Washington" });
            result.Add(new US_States() { StateId = 3, StateName = "Indiana" });
            return result;
        }
    }
}
Comments