Sunday, June 13, 2010

COM Interop with silverlight 4.0

Silverlight 4 has a feature for accessing COM components which is installed /available in client machines.
I will walk through how to access a Excel application through silvelight .
For developing this application , we need VS 2010 , Silverlight 4 SDK.
Create a silverlight project using VS2010
image
Take the properties of SL project under silverlight tab for enabling Out Of Browser support.
image
We need to check ‘require elevated permission’ under Out of browser settings button.
Now am creating a datagrid(dgName) in MainPage.xaml and binding some data.
below code is used for setting datasource to datagrid.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
       {
           ModelClass m = new ModelClass();
           dgName.ItemsSource = m.SetDataSource();
       }
image
preview : data grid.
Now i am putting a button which invokes ‘excel’ application and write all the data from the above data grid to the excel app.
private void Button_Click(object sender, RoutedEventArgs e)
       {
           dynamic excelObj = AutomationFactory.CreateObject("Excel.Application");
           excelObj.Visible = true;
           // add a workbook to the instance  
           dynamic workbook = excelObj.workbooks;
           workbook.Add();
           dynamic sheet = excelObj.ActiveSheet;
           sheet.Name = "Sample Sheet";
           // get the active sheet  
           dynamic cell = null; int i = 1;
           // iterate through our data source and populate the excel spreadsheet 
           foreach (ModelClass item in dgName.ItemsSource)
           {
               cell = sheet.Cells[i, 1]; // row, column   
               cell.Value = item.ID;
               cell.ColumnWidth = 25;
               cell = sheet.Cells[i, 2];
               cell.Value = item.Name;
               i++;
           }
       }
we are using AutomationFactory ( static class under System.Windows.Interop) for invoking Excel app . we can use this class for invoking apps like ‘Outlook’ , ‘Text to Speech’ COMs’.
‘dynamic’ is object whose operations will be resolved at run time.Intellisence will not be available while coding using ‘dynamic’ object.
image
I am attaching a sample application , can be download from here

Sunday, June 6, 2010

Bing Map integration with ASP.Net



Describes a sample application for showing your location using bing map api.
Step 1 .
Create an ASP.Net web site.
Step 2.
Include <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script> in header part of aspx page.
Include these functions for initializing and locating bing map
<script type="text/javascript">
      var map;       
      function LoadMap() {
          map = new VEMap('myMap');
          map.LoadMap();
      }
      function StartGeocoding(address) {
          map.Find(null,    // what
                      address, // where
                      null,    // VEFindType (always VEFindType.Businesses)
                      null,    // VEShapeLayer (base by default)
                      null,    // start index for results (0 by default)
                      1,    // max number of results (default is 10)
                      null,    // show results? (default is true)
                      null,    // create pushpin for what results? (ignored since what is null)
                      null,    // use default disambiguation? (default is true)
                      null,    // set best map view? (default is true)
                      GeocodeCallback);  // call back function
      }
      function GeocodeCallback(shapeLayer, findResults, places, moreResults, errorMsg) {
          // if there are no results, display any error message and return
          if (places == null) {
              //alert((errorMsg == null) ? "There were no results" : errorMsg);
              return;
          }
          var bestPlace = places[0];
          // Add pushpin to the *best* place
          var location = bestPlace.LatLong;
          var newShape = new VEShape(VEShapeType.Pushpin, location);
          //var desc = "Latitude: " + location.Latitude + "<br>Longitude:" + location.Longitude;
          newShape.SetTitle(bestPlace.Name);
          map.AddShape(newShape);
      }
      function locate()
      {
        var obj= document.getElementById('txtAddress');
        if(obj !=null)
        {
        LoadMap();
        StartGeocoding(obj.value);
        }
      }
</script>
Step 3 .
Create a div area for displaying map control.
add a text box for accepting address and a button for invoking locate() JS function included in the page.


Sample application can be downloaded from here