Saturday, December 27, 2014

XAML Broswer Applications: How to connect to Oracle database using XAML and DataGrid

XAML Browser Applications (XBAP) are Windows Presentation Foundation applications that are hosted and run inside Internet Explorer. Hosted applications run in a partial trust sandbox environment and are not given full access to the computer's resources. XBAP runs in an out-of-process executable (PresentationHost.exe) managed by a virtual machine and they are restricted to the same set of permission granted to any InternetZone application.

The question is why XBAP and not ASP.NET. The answer is simply to bring the power and rich client controls of WPF to the web using a new declarative programming model called "Extensible Application Markup Language" or XAML as well as other benefits such as:
  • Data Binding: robust way of getting data into the UI that allows developers to keep business logic and UI separated.WPF controls support data binding to information on a server, Web developers can asynchronously consume their data and visualize it using a rich data templating system.
  • 3-D: The WPF 3-D system is fully integrated into the platform.
  •  Flow Documents: flow documents dynamically layout content based on window size, device resolution, user preference, and more personalized, reading experience. 
  • Animations:  Full integration with the property and eventing systems, databinding, styling, and templating allows for deep support of rich, interactive applications. 
  • Vector graphics. Native use of vector graphics allows users to operate at any scale or resolution. 
  • Hardware acceleration. XBAP can take advantage of WPF's hardware acceleration support to create new levels of visual complexity while leaving the CPU free for the application's computing requirements.
  • Security sandbox. XBAP run in a security sandbox that limits the dangerous things like access the registry, read or write directly to the file system.
  • No-touch deployment. Because XBAP are sandboxed non-installed applications, they do not require user interaction to launch: there is no need to click through a security prompt or an information bar message. They just run.
  • .NET Framework programming languages. WPF is built on top of the .NET Framework. XBAPs receive all the benefits of being strongly typed managed applications. 
  • Same programming model for desktop/Web. Many product teams are forced to author two independent versions of their applications: a Web and a desktop version. Because WPF offers support for both online and installed applications, desktop and Web applications can share code bases. 
 In this article we will be seeing how to connect to the Oracle database using WPF Browser and will perform a search to retrieve the data from the database, display them in the data grid using Visual Studio 2013. In the Oracle database we will be having a table ADMIN_EMP with three columns EMPNO, ENAME, and DEPTNO.

Steps Involved:

A. Create a new WPF Browser Application project

  1. Start Visual Studio 2013.
  2. On the File menu, point to New, and then select Project.
    The New Project dialog box appears.
  3. In the Installed Templates pane, expand Visual Basic or Visual C#, and then select Windows.
  4. Above the middle pane, select the target framework from the drop-down list.
  5. In the middle pane, select the WPF Browser Application template.
B. Replace  Page1.xaml.cs with following code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Oracle.DataAccess.Client;
using System.Linq;
using System.Runtime.Serialization;
using System.ComponentModel;


namespace WpfBrowserApplication1
{

    public class DataModel

    {
                public ICollectionView Customers { get; private set; }
          public DataModel()
        {
               List<Employee> employees = new List<Employee>();

            string connectionString = "User Id=myuserid;Password=mypassword;Data Source=ORCL";
            using (OracleConnection connection = new OracleConnection())
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                OracleCommand command = connection.CreateCommand();
                string sql;

                sql = "SELECT * FROM admin_emp";
                

                command.CommandText = sql;
                OracleDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Employee employee = new Employee();
                    employee.EMPNO = Convert.ToInt32(reader["EMPNO"]);
                    employee.EName = Convert.ToString(reader["ENAME"]);
                    employee.DeptNo = Convert.ToInt32(reader["DEPTNO"]);
                    employees.Add(employee);
                }
            }
            Customers = CollectionViewSource.GetDefaultView(employees);
            
        }


    }
    public class Employee
    {
        public int _EMPNO;
        public string _EName;
        public string _Comm;
        public int _DeptNo;


        public int EMPNO
        {
            get { return _EMPNO; }
            set { _EMPNO = value; }
        }

        public string EName
        {
            get { return _EName; }
            set { _EName = value; }
        }

        public string Comm
        {
            get { return _Comm; }
            set { _Comm = value; }
        }

        public int DeptNo
        {
            get { return _DeptNo; }
            set { _DeptNo = value; }
        }
    }



    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {

        public Page1()
        {
                     
            InitializeComponent();         
            DataContext = new DataModel();
        }
    }
}

C. Replace  Page1.xaml with following code

<Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfBrowserApplication1" x:Class="WpfBrowserApplication1.Page1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page1">
    <Grid>
           <DataGrid x:Name="dataGrid1" Margin="0,144,175,0" VerticalAlignment="Top" Height="128" HorizontalAlignment="Right" Width="115" ItemsSource="{Binding Customers}"
  SelectionMode="Extended" SelectionUnit="Cell" />
        </Grid>            
</Page>

D. Build and Run the code
When you run the XBAP project, it opens in a browser window instead of a stand-alone window. When you debug the XBAP from Microsoft Visual Studio, the application runs with Internet zone permission and will therefore throw security exceptions if those permissions are exceeded.

E. Deploy: 
When you build an XBAP, the output includes the following three files:

Executable (.exe)This contains the compiled code and has an .exe extension.
Application manifest (.manifest)This contains metadata associated with the application and has a .manifest extension.
Deployment manifest (.xbap)This file contains the information that ClickOnce uses to deploy the application and has the .xbap extension.

Create a web site on your IIS and copy .exe, .xbap and .manifest files under the bin directory of your project file to c:\wwwroot\yourwebsite\

F. Test in Action: 

Create an html  file under c:\wwwroot\yourwebsite\ to point to your xbap file. 

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <a href="WpfBrowserApplication1.xbap">Click this link to launch the application</a></body>
</html>

Note: I'm getting the System.Security.SecurityException The demand was for: <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true"/> when attempting to create an AppDomain with restricted permissions.

No comments:

Post a Comment