A look on Database [SQL Azure] option of new Windows Azure portal

Windows Azure Development portal has been moved to new Silverlight based portal. It has all new look and many new features.

SQL Azure can be explored by selecting Database option from the left panel.

clip_image001

New SQL Azure portal is having very nice Silverlight based user interactive UI and many more operations can be performed through the UI. There is new database manager and it allows us to perform operations at table and row level.

Now we can perform many more operations through Database option of new Windows azure portal.

1. Create a database

2. Create/ delete a table

3. Create/edit/delete rows of table.

4. Create/edit stored procedure

5. Create/edit views

6. Create / execute queries etc. . . .

Let us have a walkthrough on various operations we can perform on Database option of new Windows Azure portal.

To create new Database, select Data base server and from Database section, select Create option.

clip_image003

You will get the below popup, select subscription and server from drop down

clip_image004

Give name of the Database, select Edition and maximum size of the database.

clip_image006

Once database got created, select Manage option from top to open Data Base Manager.

clip_image008

Accept given term and conditions and click Ok.

clip_image009

After Ok, you will get popup asking password to connect to database. Provide password and click on Connect .

clip_image010

After successful connection , you will get a cube providing all the information about the database you connected.

clip_image011

You can perform all the database operations from the top ribbon. You have option to create new query, new table, new view, and new stored procedure

clip_image012

If we want to create new table, click on New Table option from ribbon. We are creating a table with name Blogger. We are giving there columns ID, Name and Technology. From drop down we can select data type for the columns .

clip_image014

Then click on save button at top,

clip_image015

You can navigate between Data Base and Table tab. You can create and delete column from here also. Once you click on save button, you can see table listed at left panel.

clip_image016

At top you can see you have option to navigate either design or data view of table. Click on Data tab to navigate to Data view.

Click on Row to add a Row in the table.

clip_image017

After adding two rows, you can see table as below. You can add and delete rows from top button also.

clip_image019

Now to execute query, select Data Base tab at top and execute query as below,

clip_image021

When you click Execute button at top, you will get output as below,

clip_image023

In this way , you can perform almost all the basic operations from new SQL Azure Data Base Manager.

Windows Azure for Developers Task 10: Creating Storage Account in new Silverlight based Windows Azure Portal

1. Login to Windows Azure Development portal.

2. After login at left panel you can see number of storage and service account in your subscription.

clip_image002

3. Select subscription and storage account option to create new Storage Account. At top left, you will get New Storage Account option. To create new storage account click on new Storage Account.

clip_image004

4. Provide all the required information,

clip_image005

a. Choose subscription.

b. Give unique name URL. It must be combination of only lower letters and numbers.

c. Choose a region, and click on Create.

5. Now you can see storage account has been created.

clip_image006

6. Click on storage account and at right side panel you can see properties.

clip_image007

In properties window we can see URL for BLOB, Queue and Table access. We will have to use these URL to access table, queue or blob.

7. To view the Primary key and Secondary key click on View Button.

clip_image008

In this way , we can create a Storage Account in new Silverlight based Azure Portal.

Windows Azure for Developers Task 9: Hosting WCF Service Role in Windows Azure Hosted Service

In Previous Article We saw How to work with WCF Service Web Role

I am going to extend previous article to publish in hosted service.

Hosting WCF Service Role in hosted service is exactly the same as we host  ASP.Net Web Role.

1. Login to Azure Development portal.

2. Create a new hosted service. If you want you can upgrade any existing hosted service to host WCF Service role also.

Read here, Creating Hosted Service in Windows Azure

3. To upgrade the hosted service ; browse locally

clip_image002

4. Right click on Azure project and select Publish.

5. Now while upgrading hosted service, select browse locally option and navigate to folder containing the files.

6. Once publishing done, you can navigate to the URL. On navigation you may get below error in browser

clip_image004

Do not be panic and append Service1.svc with URL and you will get WCF Service Web Role up and running

clip_image006

7. We can consume service exactly in same way , we consume normal WCF Service.

Windows Azure for Developers Task 8: Working with WCF Service Web Role

WCF Service Role enables us to create WCF service and host in Windows Azure. In this article, we will create a WCF Service Role and host on local development fabric and consume in a console application. In second part of this article we will move WCF Service to Azure portal.

 

To start with,

1. Create a New Project

2. Navigate to cloud tab

3. Create Windows Azure Project
4. Select WCF Service Role from given options.

 

clip_image001

 

If you give a look in solution explorer, you will find in WCF Service Role project contains exactly the same structure and files as of when you create a normal WCF Service Application. It contains ,

 

1. IService1.cs (Service Contract )
2. Service1.svc.cs ( Service definition file )
3. Web.config ( configuration for EndPoints)

 

We can modify these files accordingly for our purpose in exactly the same way; we do in usual WCF Service Application.

 

Let us modify Service Contract as below,

 

IService1.svc


using System.ServiceModel;

namespace WCFServiceWebRole1
{
 [ServiceContract]
 public interface IService1
 {

[OperationContract]
 string GetData(int value);

 }
}

 

And service definition would be,

 

Service1.svc.cs

namespace WCFServiceWebRole1
{

 public class Service1 : IService1
 {
 public string GetData(int value)
 {
 return string.Format("You entered: {0}", value);
 }

 }
}

 

 

Leave default configuration in Web.Config

 

Make sure you have set Windows Azure project as Startup project and run the application. In browser you will get below error message.

 

clip_image003

 

Ignore this message and append service.svc with URL, so now URL would be http://127.0.0.1:81/service1.svc. Service.svc is name of the service definition.
After appending you will get usual WCF Service message in browser.

 

clip_image005

 

To test this WCF Service role in a console client,

 

1. Create a console application project
2. Add Service Reference by providing URL http://127.0.0.1:81/service1.svc

 

clip_image006

 

Now we will make a normal service call,

 

Program.cs

using System;
using ConsoleApplication14.ServiceReference1;

namespace ConsoleApplication14
{
 class Program
 {
 static void Main(string[] args)
 {
 Service1Client proxy = new Service1Client();
 var result = proxy.GetData(99);
 Console.WriteLine(result);
 Console.ReadKey(true);
 }
 }
}

 

 

Now when you run you may or may not get below exception.

 

clip_image007

 

To solve above exception, we have to edit App.Config file. We need to change

 

clip_image009

 

127.0.0.1 to localhost. Because it might be the case console application is not able to resolve 127.0.0.1 . we need to change to localhost.
clip_image011

 

Now on running we will get below output

 

clip_image013

 

One behavior need to be noticed here is that sometime you may get time out exception after changing 127.0.0.1 to localhost also. In my further articles, I will drill down this unwanted behavior.

Type casting with is and as operator in C#

I have been seeing many developers are casting between two types using is operator. As we know, we do have as operator also for type casting.

For purpose of this article, we are going to use below two classes,

clip_image002

Very first let us try to understand, how is operator works?

clip_image004

So , if we are running below code , we will get output as true because ofccouse p is a Player.

clip_image006

Now let us modify code a bit and check whether p is compatible to Math class or not? Since p is instance of class Player and Player class does not have Math class in hierarchy tree, so output we will get false.

clip_image008

If we compare an object against null, we will get always an output as false.

Normally we use is operator like below,

clip_image010

In above snippet, C# checks type compatibility twice and it costs the performance. So to simplify above code and improve performance C# gives us as operator to compare.

clip_image012

as operator find type compatibility, If an object is not compatible with given type then as operator returns null.

clip_image013

Binding XML File to Data Grid in Silverlight

It is a common scenario when we need to bind or display data from XML File to Silverlight Data Grid. In this article, I have tried to demonstrate this with simple steps. What all we are going to do is

1. Download content of XML file as string using WebClient class.

2. Parse XML file using LINQ to XML

3. Bind parsed result as item source of Data Grid.

Prepare Data Source

Put your XML File in ClientBin folder. You can put XML file at any server location but in that case you will have to provide absolute URI of the XML file while downloading or reading XML file.

I have created a sample Data.xml file

Data.xml


<?xml version="1.0" encoding="utf-8" ?>
<School>
 <Student RollNumber="1" Name="John Papa" />
 <Student RollNumber="2" Name="Scott Gui" />
 <Student RollNumber="3" Name="Jessy Liberty" />
 <Student RollNumber="4" Name="Tim Huer" />
 <Student RollNumber="5" Name="Victor G" />
 <Student RollNumber="6" Name="Mahesh Chand" />
 <Student RollNumber="7" Name="Pinal Dave" />
 <Student RollNumber="8" Name="Suprotim Agarwal" />
 <Student RollNumber="9" Name="Dhananjay Kumar" />
 <Student RollNumber="10" Name="Kunal Chawudhary" />
 <Student RollNumber="11" Name="Abhijit Jana" />
 <Student RollNumber="12" Name="Shiv Prasad Koirala" />
</School>

Design xaml page

I am going to create a simple page. This page is going to have a Button and a Data Grid. On click event of the Button, Data from XML file will get bind to Data Grid.

MainPage.xaml


<UserControl xmlns:sdk= http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk
 x:Class="SilverlightApplication5.MainPage"
 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
 xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
 xmlns:d=http://schemas.microsoft.com/expression/blend/2008
 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
 mc:Ignorable="d"
 d:DesignHeight="300" d:DesignWidth="400">
 <Grid x:Name="LayoutRoot" Background="White">
 <StackPanel Orientation="Vertical" Margin="50,50,50,50">
 <Button x:Name="btnDemo" Content="Click To get Data From  XML File" Height="62" Width="362" />
 <sdk:DataGrid x:Name="grdXmlData" Height="Auto" Width="Auto" AutoGenerateColumns="True" />
 </StackPanel>
 </Grid>
</UserControl>

Down Load Data from XML File

I am using WebClient class to download content of XML file as string. On button click event downloading content of XML file.

clip_image002

Do not forget to include namespace System.Net to work with WebClient class.

Parse XML Data and bind to Data Grid

We are going to use LINQ to XML to parse data. Include Namespace System.Xml.Linq . To parse data from xml file , we have written below function .

clip_image004

Explanation

1. Function is taking string as input parameter. Here we will pass e.Result from Downloadcompletedstring event.

2. Creating an instance of XDocument by parsing string

3. Reading each descendants or element on Xml file and assigning value of each attribute to properties of Entity class (Student).

We need to create an Entity class to map the data from XML File. I am going to create a class Student with properties exactly as the same of attributes of Student Element in XML file.

Student .cs


namespace SilverlightApplication5
{
 public class Student
 {

public string RollNumber { get; set; }
 public string Name { get; set; }

}
}

For reference full source code is as below,

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace SilverlightApplication5
{
 public partial class MainPage : UserControl
 {
 List<Student> lstStudents = null;
 public MainPage()
 {
 InitializeComponent();
 btnDemo.Click += new RoutedEventHandler(btnDemo_Click);
 }

private void btnDemo_Click(object sender, RoutedEventArgs e)
 {
 WebClient client = new WebClient();
 Uri uritoXML = new Uri("Data.xml", UriKind.Relative);
 client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
 client.DownloadStringAsync(uritoXML);

}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 {

if (e.Error != null)
 {
 MessageBox.Show("There is Error Downloading Data from XML File ");
 }
 else
 {

ParseXMLFile(e.Result);
 }
 }

void ParseXMLFile(string  dataInXmlFile)
 {

lstStudents = new List<Student>();

XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
 lstStudents  = (from r in xmlDoc.Descendants("Student")
 select new Student
 {
 Name = (string) r.Attribute("Name").Value,
 RollNumber =(string) r.Attribute("RollNumber").Value
 }).ToList();

grdXmlData.ItemsSource = lstStudents;
 }

}
}

On running we will get output as below,

clip_image006

Windows Azure for Developers Task 7: Creating Hosted Service in new Silverlight based Windows Azure Portal

Windows Azure Development portal is in new look now.  It is Silverlight based and all operation can be performed from one page. New portal is having Ribbon at top, left panel and right panel. From configuring guest Operating system to stopping or restarting service can be performed from one page using new portal. In this article we will walkthrough, how could we create new Hosted Service in new Azure Portal?

Follow the steps below,

1. Log in to Azure Portal

2. On top left click on New Storage Account

clip_image002

3. You will get below Popup Window to create new hosted service.

clip_image003

4. Very first choose the Subscription from drop down. I have two subscription listed there. I am choosing debugmode subscription.

clip_image004

5. Next name of the service need to be provided. You are free to provide any name for the hosted service. I am providing here name HosteTempService of the service. After provide URL of the service. I am providing URL as HosteTempService.cloudapp.net . This hosted service will be accessible at http://debugmode.cloudapp.net/

clip_image005

6. After providing Service name and URL , we need to choose the region or affinity group . So far Microsoft provided 9 regions. If you want you can create your own Affinity group here also.

clip_image006

To create Affinity group, select Create or Choose affinity group radio button .

clip_image007

From drop down select, create a new affinity group option. Give name of the Affinity group and select the desired location for affinity group.

clip_image008

7. Select the Deployment option. You can choose option not to deploy also. Later you can configure the deployment. Check the checkbox, if you want to start newly created service just after successful deployment.

clip_image009

8. Give name of the Deployment. Select configuration file and package file of the application to be hosted in this service and click on Ok button.

clip_image010

In later posts we will see how we could add a certificate in hosted service. After clicking on Ok button you will see newly created hosted service is listed.