Articles → LIGHT SWITCH 2011 → Get data from RIA WCF service in lightswitch 2011

Get data from RIA WCF service in lightswitch 2011






Objective of this tutorial





Prerequisites































Steps Involved



















Step 1. Create a new lightswitch project















Click to Enlarge




Click to Enlarge


Step 2. Create a WCF RIA service













Click to Enlarge




Click to Enlarge


Step 3. Create a Domain Service











Click to Enlarge






Click to Enlarge





namespace MyRIAService
{
    using System.ComponentModel.DataAnnotations;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Linq;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections.Generic;
    using System;


    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class MyService : DomainService
    {
    }
}



Step 4. Create a Database Table





Click to Enlarge




Click to Enlarge


Step 5. Add a method to get data from table.




public class Person
{
        [Key]
        public int PersonID { get; set; }
        public string PersonName { get; set; }
        public string PersonAddress { get; set; }
}






[Query(IsDefault = true)]
public IQueryable<Person> GetPerson()
{
   List<Person> list = new List<Person>();

   using (SqlConnection conn = new SqlConnection("conn_string"))
   {
      conn.Open();
      using (SqlDataAdapter adap = new SqlDataAdapter("Select * from Person", conn))
      {
                    DataSet ds = new DataSet();
                    adap.Fill(ds);

                    if (ds != null)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            Person info = new Person();
                            info.PersonID = Convert.ToInt32(row["person_id"]);
                            info.PersonName = Convert.ToString(row["person_name"]);
                            info.PersonAddress = Convert.ToString(row["person_address"]);

                            list.Add(info);
                        }
                    }
                }
      }

      return list.AsQueryable();
    }
}





Step 6. Use RIA WCF service in lightswitch application













Click to Enlarge








Click to Enlarge










Click to Enlarge






Click to Enlarge




Output



Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, February 15, 2014

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250