Articles → SILVERLIGHT → Add, Update And Delete Data Using RIA WCF Service
Add, Update And Delete Data Using RIA WCF Service
Software Requirement
- Visual studio 2010 is installed on your machine.
- WCF RIA Services SP2 for Silverlight 4 and 5 are installed on your machine.
- WCF RIA Services Toolkit is installed on your machine.
Prerequisite Knowledge
- How to create tables in SQL Server databases?
- What is RIA WCF service?
- How to create RIA WCF service?
Creation Of A Data Source
Click to Enlarge
Creation Of RIA WCF Service
Click to Enlarge
Add Data Into Database
// Writing this code where we are calling RIA WCF service
DomainService1 service = new DomainService1();
EntityQuery < Book > query = service.GetBooksQuery();
LoadOperation < Book > op = service.Load(query, opCompleted, null);
// Calling it as a separate method
void opCompleted(LoadOperation < Book > lo) {
Book b1 = new Book();
b1.BookName = "VALUE_YOU_WANT_TO_INSERT";
service.Books.Add(b1);
service.SubmitChanges();
}
Updating Data Into Database
// Writing this code where we are calling RIA WCF service
DomainService1 service = new DomainService1();
EntityQuery < Book > query = service.GetBooksQuery();
LoadOperation < Book > op = service.Load(query, opCompleted, null);
// Calling it as a separate method
void opCompleted(LoadOperation < Book > lo) {
foreach(Book b in service.Books) {
if (b.BookId == "BOOK_ID") {
b.BookName = "VALUE_YOU_WANT_TO_UPDATE";
service.SubmitChanges();
break;
}
}
}
Deleting Data From Database
// Writing this code where we are calling RIA WCF service
DomainService1 service = new DomainService1();
EntityQuery < Book > query = service.GetBooksQuery();
LoadOperation < Book > op = service.Load(query, opCompleted, null);
// Calling it as a separate method
foreach(Book b in service.Books) {
if (b.BookId == "BOOK_ID") {
service.Books.Remove(b);
service.SubmitChanges();
break;
}
}