.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Silverlight 5 WCF RIA Services - Part 3

Posted By:Mahadesh Mahalingappa       Posted Date: August 21, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article we are going to continue to explore the WCF Ria Services with Silverlight 5 and we will see how to insert data to the database.
 

Silverlight 5 WCF RIA Services - Part 3 :
 
In this article we are going to continue to explore the WCF Ria Services with Silverlight 5.
 
Now we are going to create a app where we would insert data to the database .
 
I will take up the same project again and add a new page to it .
 




I create a Data Model now as shown below :



Give it a Build now . Create a new Domain Service class as shown below :





namespace SL2wayWCFRia.Web
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using System.Data;
  using System.Linq;
  using System.ServiceModel.DomainServices.EntityFramework;
  using System.ServiceModel.DomainServices.Hosting;
   using System.ServiceModel.DomainServices.Server;
 
 
  // Implements application logic using the PublishingCompanyEntities context.
  // TODO: Add your application logic to these methods or in additional methods.
  // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
  // Also consider adding roles to restrict access as appropriate.
  // [RequiresAuthentication]
  [EnableClientAccess()]
  public class DataDomainService : LinqToEntitiesDomainService<PublishingCompanyEntities>
  {
 
  // TODO:
  // Consider constraining the results of your query method.  If you need additional input you can
  // add parameters to this method or create additional query methods with different names.
  // To support paging you will need to add ordering to the 'Articles' query.
  public IQueryable<Article> GetArticles()
  {
  return this.ObjectContext.Articles;
  }
 
  public void InsertArticle(Article article)
  {
  if ((article.EntityState != EntityState.Detached))
  {
  this.ObjectContext.ObjectStateManager.ChangeObjectState(article, EntityState.Added);
  }
  else
  {
   this.ObjectContext.Articles.AddObject(article);
  }
  }
 
  public void UpdateArticle(Article currentArticle)
  {
  this.ObjectContext.Articles.AttachAsModified(currentArticle, this.ChangeSet.GetOriginal(currentArticle));
  }
 
  public void DeleteArticle(Article article)
  {
  if ((article.EntityState != EntityState.Detached))
  {
  this.ObjectContext.ObjectStateManager.ChangeObjectState(article, EntityState.Deleted);
  }
  else
  {
  this.ObjectContext.Articles.Attach(article);
  this.ObjectContext.Articles.DeleteObject(article);
  }
  }
  }
}







Make sure that  AutoGenerateColumns="True" .

The Designer window will now look as follows :







Now lets add the code for adding an article . Here I would need to manually add the UI for inserting articles .
 


The code behind for the two buttons is provided below :

DataDomainContext context = new DataDomainContext();
 
 
  private void add_Click(object sender, RoutedEventArgs e)
  {
  try
  {
  context.Load(context.GetArticlesQuery());  // Loading the context from the database using the GetArticlesQuery .
 
// Creating a new article .
 
  Article art = new Article();
 
  art.ArticleID = 101;
  art.AuthorID = 5;
  art.Body = "This is a sample Article";
   art.Title = "This is sample Title";
 
  context.Articles.Add(art); // Adding the article to the Context
 
  MessageBox.Show("New article was sucessfully added");
 
  }
  catch (Exception ex)
  {
  MessageBox.Show("Adding article failed :" + ex.Message);
  }
 
  }
 
  private void save_Click(object sender, RoutedEventArgs e)
  {
  try
  {
  context.SubmitChanges();
   MessageBox.Show("The Changes were saved successfully to the DataBase");
  }
  catch (Exception ex)
  {
  MessageBox.Show("The changes were not saved. Try again :" + ex.Message);
  }
  }

Run the application again . This time hit the button Add and save . The following messages should appear .





Checked the database .


Hit refresh to the application .
 

This was a very simple app where we took another step towards creating a end to end Business App. In the next post we would design we would see how to validate our input .  Till then . Happy Coding . 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend