Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Monday, July 20, 2009

Pie Chart in Silverlight with WCF - (Part Final of Many)

Please see the previous post...
Lets put in the code that actually draws the pie chart in the chart control.

To show the data in our Pie Chart, we need the WCF to service reference in our Silverlight project, to do that right click on the refereces folder in the Silverlight project “PieChart” and click on “Add Service Reference”.
Below is the window, that adds service reference to our silverlight project.


Click on “Discover” and it will find our WCF service, and make sure you have the method on the right under “Operations”. Click on OK to add the WCF as Service reference to our Silverlight Project.
You can give any name to your service reference like anything related, but as this is just one Service reference I am just going by default name “ServiceReference1”.
So now make the following changes to your MainPage.xaml


Now build your project, goto Build\Build Solution, and run the program, go to
Debug\Start Without Debugging (Ctrl+F5)
It runs the program and you will get an output like below


Click on Generate Button and the final output will be below


To See all parts for Pie Chart in Silverlight with WCF, please click on the link to the right that says it.
You can download the final version of the project and full version of the post in a word document in the final part of this post.
Please do comment or leave any suggestions or questions below, I will reply immediately. You may also leave any requests for new posts.
Thank you, See you soon with a new post.

Pie Chart in Silverlight with WCF - (Part 3 of Many)

So Lets start creating a web service and calling the web service in our siverlight application. We use LINQ to SQL to access data from the database. As we just said the data is will be on the server side we need to create a web service on the server side project that Visual studio created for us “PieChart.Web”. Follow the below steps to create a web service.
1. Right click on “PieChart.Web” project and click on Add Item to add a new “LINQ to SQL” file.


You can give any name to the file, but I am just going with the default name “DataClasses1.dbml”.
Click on OK, this adds “DataClasses1.dbml” file to our Web project and also opens a window where you were asked to select the database and its tables that we wanted to use for our project.


Make sure the window is named “DataClasses1.dbml” and one the left it should say “Server Explorer” . In case if you cant see this window , you can always click on “Server Explorer” link in the center of the screen.
2. Once you have the server explorer window on the left, right click on “ Data Connections” and select Add Connection. This opens the following window where you can select the type of database


Click on Continue , which opens a new window that asks us to select the database from our database server. Which looks like this,


Select the Server Name from the list and select the database from the bottom list. Click on OK that shows the database in the “Server Explorer” window to our left.
After it shows your database in the server explorer window, you select the database and go down to select the necessary database (in our case it is ExpensesDatabase) and drag the table from the left to the right window. Which will look like below.


3. As the data is at the server side and the calls are made from the client side we need to make the talking searlizable so click on the white part of the window and in the properties window select the below selected option.
Below is how your window should look after you make the connection searlizable.


4. Now that we added the database to our project, we have to add a web service that contacts the database and sends data to us. So right click on the web project “PieChart.Web” and select Add item to add “Silverlight Enabled WCF Service”. You can give any name you want to name the service but in this case I am just going with the default name.


Click on Add to add a WCF Service to your project.

5. Right after you added the WCF Service it gives you a default code that helps you to work further on it. The default code window looks like this


6. Please copy and paste the following code in the appropriate position so that we will be ready to call our web service and use the data in our PieChart.
Code:
public ExpenseTable[] GetExpense()
{
DataClasses1DataContext expenseCTX = new DataClasses1DataContext();
var expense = from exp in expenseCTX.ExpenseTables
select exp;
return expense.ToArray();
}
And after adding the above code the Service1.svc file looks like this.


7. Now that we have our WCF Done and Database done, its time to design a simple UI, with just a Button “Generate” and a Pie Chart Control to display the data.
Follow is the code that we have to put in the MainPage.XAML file in order to get the UI done.
In Silverlight the button control can be found by default but the Pie chart control is a special user control that can be only found by adding a reference to the .dll file in our Silverlight project.
Add “System.Window.Controls.Data.dll” and “System.Window.Controls.DataVisualization.dll”.
Right click on the “PieChart\References” and select Add reference and go to “Browse” tab and browse to the following path,
C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Toolkit\Jul09\Bin\System.windows.Controls.DataVisualization.dll
If you don’t see jul09 or toolkit folder, don’t panic or feel bad about it, just download the jul09 Silverlight Toolkit.

Add the following code in the “MainPage.xml” in the between the columns. StackPanel is one of the Silverlight control that stacks various other controls that are put in the , in our case we put in a Button Control and a Chart Control in the stack panel.


This is how the above code looks when you run the program,


Checkout the Address, it shows the test project “PieChart.Web” that was created by Visual Studio.
And also look at the stack panel , how it stacked the controls up, a button and a chart control.

Next Part Continues...
To See all parts for Pie Chart in Silverlight with WCF, please click on the link to the right that says it.
You can download the final version of the project and full version of the post in a word document in the final part of this post.
Please do comment or leave any suggestions or questions below, I will reply immediately. You may also leave any requests for new posts.

Sunday, July 19, 2009

Pie Chart in Silverlight with WCF - (Part 2 of Many)

Implementation:

Open Microsoft Visual Studio 2008, go to FILE MENU and select CREATE\NEW PROJECT. Select “SilverlightApplication” on the right and make sure you window looks like below. And also make sure all the highlted fields are same and you can give any name to the project , but as we are doing a Pie Chart I named it as PieChart.



Click on OK to start working on our project. As Silverlight is a Client side application, it needs a host to run on the web browser. And below is the window how it looks (I am using Silverlight 3.0)



Make sure you have a good look at the highlighted fields, it will tell you that you need a server side host, like a web application to host the silverlight.
Points to be noticed:
1. Silverlight is a Client side application
2. It needs a server side application(like a web browser) to host the application
Click on OK to start your project.

As we selected the check box in the previous window to host our silverlight application it creates a new PieChart.Web project to our project we just created and below is how it looks in the solution explorer window and also gives us some sample code with the default namespaces and references.


The selected “PieChart.Web” is the host project that is created by Visual Studio to host our Silverlight application. And the PieChartTestPage.aspx is the file that is used to show in the browser.
And MainPage.xaml is the file that is where we do the programming for our PieChart, which is coded in XAML language. So let us just keep the “Piechart.Web” aside for a while and let’s work on MainPage.xaml.
Before we design out Pie Chart we need some data to show in the Pie chart. To draw any Pie chart we need two sets of values one set will be words and the second set will be numbers to relate the text. In Silverlight these set of values are denoted as “IndependentValueBinding” for the set of text and “DependentValueBinding” for the values set. So Lets give the data first in our database (SQL Server), Lets open SQL Sever Management Studio to create a database called “Expences” with a table called “ExpensesTable” with two attributes, Months(for IndependentValueBinding) and Expences (for DependentValueBinding).

Please see this post to create a database in SQLServer “Database Creation” (Caution: only for first timers).

As this post is for Silverlight, we are not discussing SQL Server and creating Tables in SQL Server. For any reference please see the post mentioned above.

Once you are done with creating Database and a Table, lets jump into Visual Studio to use the data for our PieChart.

Note: Silverlight is so fun to play with various builtin controls and animations etc, but when it comes to data binding and using data in our silverlight application we need a web service for the Silverlight Application to invoke data from the database.
The reason behind this is, as we know that Silverlight is a client side application and the data will always be on the server side, so we need a web service to read the server side data in our application.

Next Part Continues...
To See all parts for Pie Chart in Silverlight with WCF, please click on the link to the right that says it.
You can download the final version of the project and final version of the post in a word document in the final part of this post.
Please do comment or leave any suggestions or questions below, I will reply immediately. You may also leave any requests for new posts.

Pie Chart in Silverlight with WCF - (Part 1 of Many)

Silverlight:
This is a web development tool created by Microsoft to compete with the Adobe Flash player and its plugin. In my view it is almost getting there. To my knowledge we can do anything and everything that we can possibly do in flash (using action script).
Silverlight is quite a recent technology, released in 2007 as Silverlight 1.0 and now in just 2 year we have a very advanced version Silverlight 3.0. For me it used to be touch to program in Silverlight 1.0 where I know I can do great stuff, but the way we had to do is different than it is now. Microsoft also came up with a new suite called “Microsoft Expression” a combination of design and development tool for web designing and development. An Application called “Microsoft Expression Blend” which is a tool that makes our job much easy to develop a Silverlight Application.
Lets stop talking about the history and jump into the project.
Basics:
You can follow me in developing this application even though you don’t have any idea of Visual Studio, Silverlight, Expression Blend, XAML and WCF etc. because each and every step is explained in detail and also screenshots help you to connect the explanation to the project that we are going to do. In case if you are wondering, following are the necessary softwares that you will need to follow me with the project. You don’t have to stop reading the post even though you don’t have any of the below because, it might help you get an idea of how Silverlight and WCF mix together.
List of Softwares:
1. Visual Studio 2008 (Professional or Express Edition)

2. Microsoft Expression Blend (with SP1)

3. Microsoft SQL Server (Professional or Express Edition)

4. Silverlight 2.0 or 3.0 toolkit installed
you can download Silverlight 2.0 Toolkit here
you can download Silverlight 3.0 Toolkit here
You may also have to install Silverlight Tools for Visual Studio 2008 SP1 here

5. Silverlight SDK installed
you may download Silverlight 2.0 SDK here
if you want to upgrade to Silverlight 3.0, you can download Silverlight 3.0 Tools for Visual Studio SP1 here

Visit the following post to follow the whole project.
The Final Completed Version of the project and whole word document of this post can be downloaded from the final post. But i would encourage to follow all posts for this article(from 1 till final of many).
Please leave comments for your support and suggestions about the post and any other posts.