Articles → SILVERLIGHT → Add, Update And Delete Data Using RIA WCF Service

Add, Update And Delete Data Using RIA WCF Service






Software Requirement


  1. Visual studio 2010 is installed on your machine.
  2. WCF RIA Services SP2 for Silverlight 4 and 5 are installed on your machine.
  3. WCF RIA Services Toolkit is installed on your machine.

Prerequisite Knowledge


  1. How to create tables in SQL Server databases?
  2. What is RIA WCF service?
  3. How to create RIA WCF service?

Creation Of A Data Source




Picture showing the table schema of 'Book' table
Click to Enlarge


Creation Of RIA WCF Service




Picture showing the auto generated code of WCF RIA services
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;
	}
}



Posted By  -  Karan Gupta
 
Posted On  -  Friday, March 9, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250