Saturday, July 31, 2010    
Blog  
OpenLight Blog
Jan 9

Written by: Michael Washington
1/9/2010 2:47 PM 

image

Click here for the live example:

http://www.adefwebserver.com/silverlight/CloudDBSilverlight/Default.aspx

image

I checked Twitter yesterday and saw a post about CloudDB.com giving away free invites to their Beta test. I got an invite and logged into the service.

A lot of credit goes to the creators of a very nice slick interface. You can easily create an “Application” and then a “Entity” (basically a table) in that application.

image

I name my application Message Board, and I create an entity called Messages with a Name field and a Message field.

 image

You can even click on the table in their web based UI interface…

image

…and enter records.

image

I can now create a web service using their interface.

image

I create a ASP.NET application and I create a web reference to the web service.

I then created a web service with private methods to obtain a token and log in and log out of the database:

    static string CloudDBUsername = "your cloudDB.com username";
    static string CloudDBPassword = "your cloudDB.com password";
 
    private static Token LoginAndGetToken()
    {
        MessageBoard_1 CloudApp = new MessageBoard_1();
        TokenResponse objTokenResponse = 
            CloudApp.LogIn(CloudDBUsername, CloudDBPassword);
        return objTokenResponse.Result;
    }
 
    private static void Logout(Token LogoutToken)
    {
        MessageBoard_1 CloudApp = new MessageBoard_1();
        Outcome objOutcome = CloudApp.LogOut(LogoutToken);
    }

Next, I create web methods to display the data and allow for a record to be added:

    [WebMethod]
    public MessagesEntity[] GetRecords(int pageNumber)
    {
        Token objToken = LoginAndGetToken();
 
        Pager objPager = new Pager();
        objPager.PageNumber = (pageNumber + 1);
        objPager.RowsPerPage = 100;
 
        MessageBoard_1 CloudApp = new MessageBoard_1();
        MessagesEntityListResponse colMessagesEntityListResponse = 
            CloudApp.GetMessagesList(objToken, 0, objPager);
        Logout(objToken);
 
        return colMessagesEntityListResponse.Result;
    }
 
    [WebMethod]
    public void SaveRecord(string Name, string Message)
    {
        Token objToken = LoginAndGetToken();
 
        MessagesEntity[] objMessagesEntity = new MessagesEntity[1];
        objMessagesEntity[0] = new MessagesEntity();
        objMessagesEntity[0].nameText = Name;
        objMessagesEntity[0].messageText = Message;
 
        MessageBoard_1 CloudApp = new MessageBoard_1();
        CloudApp.SaveMessages(objToken, objMessagesEntity);
 
        Logout(objToken);
    }

 

Silverlight Application

image

Next, I create a Silverlight application that communicates with the web service methods I just created. To keep things simple I did not employ proper MVVM binding and such and just coded the app in the “down and dirty” method.

image

I create a UI in Expression Blend that consits of a datagrid, two pager buttons, text boxes for the Name and the Message, and a button to add a message.

image

I create a web service reference to the web service I just created.

I create this method that will get the address of the website that the Silverlight application is currently running on:

        private string GetWebserviceAddress()
        {
            string strXapFile = @"/ClientBin/CloudDBSilverlight.xap";
            string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri.Replace(strXapFile, "");
            return string.Format(@"{0}/{1}", strBaseWebAddress, "WebService.asmx"); 
        }

 

These methods get the current Message Board items and fill the datagrid:

        private void GetRecords(int PageNumber)
        {
            WebServiceSoapClient CloudDBApp = new WebServiceSoapClient();
            EndpointAddress WebServiceEndpointAddress = new EndpointAddress(GetWebserviceAddress());
            CloudDBApp.Endpoint.Address = WebServiceEndpointAddress;
            CloudDBApp.GetRecordsCompleted +=
                new EventHandler(CloudDBApp_GetRecordsCompleted);
            CloudDBApp.GetRecordsAsync(PageNumber);
            txtBusy.Visibility = Visibility.Visible;     
        }
 
        void CloudDBApp_GetRecordsCompleted(object sender, GetRecordsCompletedEventArgs e)
        {
            txtBusy.Visibility = Visibility.Collapsed;     
            colMessages = new List();
            foreach (var item in e.Result)
            {
                Message objMessage = new Message();
 
                objMessage.Name = item.nameText;
                objMessage.Comment = item.messageText;
 
                colMessages.Add(objMessage);
            }
 
            dgMessages.ItemsSource = colMessages;
 

This method handles adding a new message:

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            WebServiceSoapClient CloudDBApp = new WebServiceSoapClient();
            EndpointAddress WebServiceEndpointAddress = new EndpointAddress(GetWebserviceAddress());
            CloudDBApp.Endpoint.Address = WebServiceEndpointAddress;
            CloudDBApp.SaveRecordCompleted += 
                new EventHandler(CloudDBApp_SaveRecordCompleted);
            CloudDBApp.SaveRecordAsync(txtName.Text, txtMessage.Text);
            txtName.Text = "";
            txtMessage.Text = "";
            txtBusy.Visibility = Visibility.Visible;        
        }
 
        void CloudDBApp_SaveRecordCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            txtBusy.Visibility = Visibility.Collapsed; 
            GetRecords(currentPage);
        }

 

Click here for the live example:

http://www.adefwebserver.com/silverlight/CloudDBSilverlight/Default.aspx

 

Download the source code here:

http://adefwebserver.com/silverlight/CloudDBSilverlight/CloudDBSilverlight.zip

Note: In order to run the source code, you will need to:

1) Create a cloud.DB.com account

2) Create a application called Message Board

3) Create an Entity called Messages with a text field called Name and a text field called Message

4) Update the web service connection in the ASP.NET site to point to your cloudDB web service (delete the one that is in the source code and use “Add Web Reference…”)

5) Update the code at the top of the WebService.cs file with your cloudDB username and password

Tags:

4 comment(s) so far...

Re: Silverlight Message Board using the CloudDB.com “cloud” database

Hey!

Your link to the live example doesn't work for me. You need to remove the 'www.'.

Thanks for the blogpost!

By Michael van Rooijen on   1/11/2010 9:44 AM

Re: Silverlight Message Board using the CloudDB.com “cloud” database

@Michael van Rooijen - It should work if you go to the address with the "www." before the adress or without. can you get to www.adefwebserver.com ?

By Michael Washington on   1/11/2010 9:45 AM

Re: Silverlight Message Board using the CloudDB.com “cloud” database

Nice article Michael!

I've had this subject on my todo list for almost 1 year :-) (I logged the idea on January 14th 2009). Good coverage!

By Mark Monster on   1/11/2010 11:03 AM

Re: Silverlight Message Board using the CloudDB.com “cloud” database

@Mark Monster - Thanks for the feedback!

By Michael Washington on   1/13/2010 5:48 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use