Articles → LIGHT SWITCH 2011 → Paging In Datagrid In Lightswitch When Binding From Stored Procedure
Paging In Datagrid In Lightswitch When Binding From Stored Procedure
Objective Of This Tutorial
- Visual studio 2010 is installed on your machine.
- Visual Studio 2010 service pack 1 is installed on your machine.
- Microsoft Visual Studio Light switch 2011 is installed on your machine.
- WCF RIA Services Toolkit is installed on your machine.
- How to create a project in Visual studio 2010?
- How to add new item in the project?
- How to add new project in the existing solution?
- Understanding of WCF RIA Service
- What is a ‘Class Library’ project?
- Understanding about lightswitch
- Basic knowledge of SQL server
- Basics of ado.net(How to call a stored procedure in C# code)
Steps Involved
- Create projects
- Create table in the database.
- Add a stored procedure to get records
- Run the Application
Step 1. Create Projects
- Please follow the steps mentioned below
- Create a new lightswitch project by the name of ‘Paging_using_stored_procedure’.
- Create a new class library by the name of ‘MyRIAService’.
- From ‘MyRIAService’, delete ‘Class1.cs’
- Add a new ‘Domain Service Class’ in ‘MyRIAService’.
Click to Enlarge
Step 2. Create Table In The Database.
Click to Enlarge
Click to Enlarge
Step 3. Add A Stored Procedure To Get Records
CreateProc GetClasses As
SELECT
[ClassId],
[ClassCode],
[ClassDisplayName]
FROM
[dbo].[Class]
namespace MyRIAService {
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Data.SqlClient;
using System.Data;
public class Class { [Key]
public int ClassId {
get;
set;
}
public string ClassCode {
get;
set;
}
public string ClassDisplayName {
get;
set;
}
}
// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class MyDomainService: DomainService { [Query(IsDefault = true)]
public IQueryable < Class > GetClasses() {
List < Class > lst = new List < Class > ();
DataSet ds = new DataSet();
using(SqlConnection connection = new SqlConnection(@"conn_string")) {
using(SqlCommand cmd = new SqlCommand("GetClasses", connection)) {
connection.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
using(SqlDataAdapter adapter = new SqlDataAdapter(cmd)) {
adapter.Fill(ds);
if (ds != null) {
if (ds.Tables.Count > 0) {
foreach(DataRow row in ds.Tables[0].Rows) {
Class cls = new Class();
cls.ClassId = Convert.ToInt32(row["ClassId"]);
cls.ClassCode = Convert.ToString(row["ClassCode"]);
cls.ClassDisplayName = Convert.ToString(row["ClassDisplayName"]);
lst.Add(cls);
}
}
}
}
}
}
return lst.AsQueryable();
}
}
}
Click to Enlarge
Step 4: Run The Application
Click to Enlarge
protected override int Count < T > (IQueryable < T > query) {
return query.Count();
}
namespace MyRIAService {
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Data.SqlClient;
using System.Data;
public class Class { [Key]
public int ClassId {
get;
set;
}
public string ClassCode {
get;
set;
}
public string ClassDisplayName {
get;
set;
}
}
// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class MyDomainService: DomainService { [Query(IsDefault = true)]
public IQueryable < Class > GetClasses() {
List < Class > lst = new List < Class > ();
DataSet ds = new DataSet();
using(SqlConnection connection = new SqlConnection(@"Data Source=.\SQLExpress2K8R2;Database=DynamicData;Integrated Security=true;")) {
using(SqlCommand cmd = new SqlCommand("GetClasses", connection)) {
connection.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
using(SqlDataAdapter adapter = new SqlDataAdapter(cmd)) {
adapter.Fill(ds);
if (ds != null) {
if (ds.Tables.Count > 0) {
foreach(DataRow row in ds.Tables[0].Rows) {
Class cls = new Class();
cls.ClassId = Convert.ToInt32(row["ClassId"]);
cls.ClassCode = Convert.ToString(row["ClassCode"]);
cls.ClassDisplayName = Convert.ToString(row["ClassDisplayName"]);
lst.Add(cls);
}
}
}
}
}
}
return lst.AsQueryable();
}
protected override int Count < T > (IQueryable < T > query) {
return query.Count();
}
}
}
Click to Enlarge
Posted By - | Karan Gupta |
|
Posted On - | Thursday, March 20, 2014 |