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

Add, update and delete data using RIA WCF service






















Software requirement











Prerequisite knowledge











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 09, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250