Articles → SILVERLIGHT → Add, update and delete data using RIA WCF service

Add, update and delete data using RIA WCF service



  1. Software requirement.
  2. Prerequisite knowledge
  3. Creation of a data source.
  4. Creation of RIA WCF service.
  5. Add data into database.
  6. Updating data into database
  7. Deleting data from database.



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