Friday, 9 December 2011

When Should I use WPF Vs Silverlight

In my role, I work with a number of large corporations, and this is a question that they ask me regularly: What is the difference between the Windows Presentation Foundation (WPF) and Silverlight, and in what scenarios does it make sense to use each?
Microsoft feels that user experience is important, and invested in multiple technologies to promote better user experience. Both WPF and Silverlight use XAML (Extensible Application Markup Language) under the covers.
Let's look at some of the different characteristics of each technology:

WPF:
  • Ships as part of the .NET Framework (version 3.0 and onward)
  • Runs as Windows application or as web "browser application" (called XBAP, for "XAML Browser Application"). Note that XBAPs run only in Internet Explorer with .NET 3.0 and in both Internet Explorer and Firefox with .NET 3.5.
  • Runs on Windows machines only (Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008)
  • Richest functionality, including 3D graphics
Silverlight:
  • Ships independently
  • Runs in web browsers only (Internet Explorer, Firefox, Safari)
  • Runs on Windows or Mac operating systems (also on Linux via Moonlight, which is an open source implementation of Silverlight based on Mono)
  • Functionality is a subset of WPF's feature set

When should you use each? The maddening answer is (of course): it depends!
WPF is a more mature technology and was designed with a richer feature set. It also has the advantage of being able to run in a browser or as an installed Windows-Form-type app.
Silverlight has a broader reach. You can access Silverlight from many operating systems and web browsers.
The most important reason to choose one over the other should be based on the intended audience for the application. For example, if a corporation is designing an application for internal use only and every employee has Windows XP as the company standard OS, then go with WPF to leverage the richer feature set. If a corporation is designing an external-facing website, then Silverlight is the better choice because potential customers can access the website from a variety of different operating systems and browsers.

Saturday, 13 August 2011

Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Microsoft Entity Framework version 4.0 is a brand new ORM(Object Relational Mapper) from Microsoft. It’s provides now some new features which are not there in the earlier version of Entity framework. Let’s walk through a simple example of a new features which will create a new Entity class based on stored procedure result. We will use same table for this example for which they have used earlier for Linq Binding with Custom Entity.
Below is the table which have simple fields like first name,last name etc.SQL Table for Entity Example.
Let’s insert some data like following.
Sample data for Entity Framework Example
Below is the stored procedure which I am going to use for this example which will simply fetch data from the table.
1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE PROCEDURE dbo.GetAllUsers
AS
SET NOCOUNT ON
SELECT
UserId,
UserName,
FirstName,
LastName,
FirstName + ' ' + LastName AS [FullName]
FROM dbo.Users


Now let’s create Entity model class just like below via Project->Add New Item->Go to Data tab and then select ADO.NET Entity Data Model.
Adding a New Enity model class for Entity Framework example
Once you click add a dialog box will appear which will ask for Data Model Contents there are two options Generate From Database and another one is Empty Model. Generate from database will create a Entity model from the ready made database while Empty model enable us to create a model first and then it will allow us to create a database from our model. We are going to use Generate From database for this example. Select Generate From Database like following and click Next.
Generating Model from database for Entity Framework 4.0 Example
After click on the next it will ask for database connection string like following where you need to apply connection string for that. I am already having connection string in my web.config so i just selected like below otherwise you need to create one via clicking on new connection. Also you need to specify the connection string name in web.config so it will put a connection string in connection string section with that name.
MyConnection
Once you click next it will fetch the all database objects information like Tables,Views and Stored Procedure like following. Here our purpose is to work with stored procedure so I just selected the stored procedure and selected the GetAllUsers Stored Procedure.
Selecting Stored procedure
After that it will create a Entity Model class in solution explorer.Now we want to bind the stored procedure with result class so first we need to create function which will call ‘GetAllUser’ stored procedure. To create function we just need to select Our Entity Model class and then go to Model Browser and select the Stored Procedure and right click->Add Function Import like following image.
Importing a function from stored procedure example.
It will start a new wizard and will go to next step like following image which will have four options 1. None 2. Scalars 3. Complex 4. Entities and there will be a button Get Column information once you click it. It will gather all the column information of stored procedure result set and then click ‘Create New Complex Type’ It will create a new complex type from the gathered column information.
Gathering a column info and then
You can also rename that class so I have renamed the class as ‘User Info’ like following.UserInfo
Now click ok and now it will create function which call the stored procedure and will return Object Result set of Type ‘UserInfo’ which we have just created. Now let’s bind that to a simple grid view to see how its works. So Let’s take a simple grid view like below.
1
2
<asp:GridView ID="grdUserList" runat="server">
</asp:GridView>
Now Let’s bind that grid view like following from the code behind file of asp.net like following.
?
1
2
3
4
5
6
7
8
9
10
11
12
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (BlogEntities myEntity = new BlogEntities())
{
grdUserList.DataSource = myEntity.GetAllUsers();
grdUserList.DataBind();
}
}
}

Just run it with Ctrl + F5 and below it the output in browser.Output of Entity Framework 4.0 Example

That’s it very easy and simple to bind complex type with stored procedure using ADO.NET Entity Framework 4.0. Hope this will help you.. Happy Programming!!!

Sunday, 24 July 2011

SQL Server Enable xp_cmdshell Option

Introduced in SQL Server 2005, the xp_cmdshell option is a server configuration option that enables system administrators to control whether the xp_cmdshell extended stored procedure can be executed on a system. By default, the xp_cmdshell option is disabled on new installations and can be enabled by using the Policy-Based Management or by running the sp_configure system stored procedure as shown in the following code example:


-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

Friday, 22 July 2011

SplineSeries for the Silverlight Toolkit - C# Code

While the Silverlight Toolkit has made some amazing strides in improving, it still lacks some functionality compared to other toolkit offerings. In particular, Silverlight Toolkit's charts lack splines, or smoothed lines. The idea here is that we don't want the graph to appears as though a line is moving from point to point directly -- that it moves parabolically, or softer.




A typical line series.



Now, this is all fun and games, but can we get something where transitions are more smooth? Yes, we can.




A spline series.



Notice, the spline series changes are more gradual and curved. Are they an entirely accurate representation of the in-between? No, not always -- but they're not meant to be (necessarily).



The Spline Series is literally a copy/paste of the LineSeries, with a few changes. Note that this is located under the Charting/Series folder of the DataVisualization.Toolkit project. Essentially, what we want to do is change the Polyline to a Path. The idea here is that a Polyline doesn't draw Beziers. Below is my (large) code,



SplineSeries.cs:



// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
 
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Windows.Media;
using System.Windows.Shapes;
 
#if !DEFINITION_SERIES_COMPATIBILITY_MODE
 
namespace System.Windows.Controls.DataVisualization.Charting
{
    /// <summary>
    /// Represents a control that contains a data series to be rendered in X/Y 
    /// line format.
    /// </summary>
    /// <QualityBand>Preview</QualityBand>
    [StyleTypedProperty(Property = DataPointStyleName, StyleTargetType = typeof(LineDataPoint))]
    [StyleTypedProperty(Property = "LegendItemStyle", StyleTargetType = typeof(LegendItem))]
    [StyleTypedProperty(Property = "PathStyle", StyleTargetType = typeof(Path))]
    [TemplatePart(Name = DataPointSeries.PlotAreaName, Type = typeof(Canvas))]
    [SuppressMessage("Microsoft.Maintainability", "CA1501:AvoidExcessiveInheritance", Justification = "Depth of hierarchy is necessary to avoid code duplication.")]
    public partial class SplineSeries : LineAreaBaseSeries<LineDataPoint>
    {
        #region public PointCollection Points
        /// <summary>
        /// Gets the collection of points that make up the spline.
        /// </summary>
        public PointCollection Points
        {
            get { return GetValue(PointsProperty) as PointCollection; }
            private set { SetValue(PointsProperty, value); }
        }
 
        /// <summary>
        /// Identifies the Points dependency property.
        /// </summary>
        public static readonly DependencyProperty PointsProperty =
            DependencyProperty.Register(
                "Points",
                typeof(PointCollection),
                typeof(SplineSeries),
                null);
        #endregion public PointCollection Points
 
        #region public PathGeometry SplinePoints
        /// <summary>
        /// Gets the collection of points that make up the line.
        /// </summary>
        public PathGeometry SplinePoints
        {
            get { return GetValue(SplinePointsProperty) as PathGeometry; }
            private set { SetValue(SplinePointsProperty, value); }
        }
 
        /// <summary>
        /// Identifies the SplinePoints dependency property.
        /// </summary>
        public static readonly DependencyProperty SplinePointsProperty =
            DependencyProperty.Register(
                "SplinePoints",
                typeof(PathGeometry),
                typeof(SplineSeries),
                null);
        #endregion public PathGeometry SplinePoints
 
        #region public double SplineTension
 
        /// <summary>
        /// Gets or sets the tension in the beziers that make up the spline.
        /// </summary>
        /// <remarks>
        /// The greater the tension, the more straight/linear the spline will look.
        /// Less tension creates a more curvy spline.
        /// </remarks>
        public double SplineTension
        {
            get { return (double) GetValue(SplineTensionProperty); }
            set { SetValue(SplineTensionProperty, value); }
        }
 
        /// <summary>
        /// Identifies the SplineTension dependency property.
        /// </summary>
        public static readonly DependencyProperty SplineTensionProperty =
            DependencyProperty.Register(
                "SplineTension",
                typeof(double),
                typeof(SplineSeries),
                new PropertyMetadata(2.5));
        #endregion public double SplineTension
 
        #region public Style PathStyle
        /// <summary>
        /// Gets or sets the style of the Path object that follows the data 
        /// points.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Path", Justification = "Matches System.Windows.Shapes.Path.")]
        public Style PathStyle
        {
            get { return GetValue(PathStyleProperty) as Style; }
            set { SetValue(PathStyleProperty, value); }
        }
 
        /// <summary>
        /// Identifies the PathStyle dependency property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Path", Justification = "Matches System.Windows.Shapes.Path.")]
        public static readonly DependencyProperty PathStyleProperty =
            DependencyProperty.Register(
                "PathStyle",
                typeof(Style),
                typeof(SplineSeries),
                null);
        #endregion public Style PathStyle
 
#if !SILVERLIGHT
        /// <summary>
        /// Initializes the static members of the LineSeries class.
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Dependency properties are initialized in-line.")]
        static SplineSeries()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SplineSeries), new FrameworkPropertyMetadata(typeof(SplineSeries)));
        }
 
#endif
        /// <summary>
        /// Initializes a new instance of the LineSeries class.
        /// </summary>
        public SplineSeries()
        {
#if SILVERLIGHT
            this.DefaultStyleKey = typeof(SplineSeries);
#endif
        }
 
        /// <summary>
        /// Acquire a horizontal linear axis and a vertical linear axis.
        /// </summary>
        /// <param name="firstDataPoint">The first data point.</param>
        protected override void GetAxes(DataPoint firstDataPoint)
        {
            GetAxes(
                firstDataPoint,
                (axis) => axis.Orientation == AxisOrientation.X,
                () =>
                {
                    IAxis axis = CreateRangeAxisFromData(firstDataPoint.IndependentValue);
                    if (axis == null)
                    {
                        axis = new CategoryAxis();
                    }
                    axis.Orientation = AxisOrientation.X;
                    return axis;
                },
                (axis) => axis.Orientation == AxisOrientation.Y && axis is IRangeAxis,
                () =>
                {
                    DisplayAxis axis = (DisplayAxis)CreateRangeAxisFromData(firstDataPoint.DependentValue);
                    if (axis == null)
                    {
                        throw new InvalidOperationException(Properties.Resources.DataPointSeriesWithAxes_NoSuitableAxisAvailableForPlottingDependentValue);
                    }
                    axis.ShowGridLines = true;
                    axis.Orientation = AxisOrientation.Y;
                    return axis;
                });
        }
 
        /// <summary>
        /// Updates the Series shape object from a collection of Points.
        /// </summary>
        /// <param name="points">Collection of Points.</param>
        protected override void UpdateShapeFromPoints(IEnumerable<Point> points)
        {
            if (points.Any())
            {
                PointCollection pointCollection = new PointCollection();
                foreach (Point point in points)
                {
                    pointCollection.Add(point);
                }
 
                //At least two points are necessary to generate a proper spline
                if (pointCollection.Count >= 2)
                {
                    PathGeometry geometry = new PathGeometry();
                    PathFigure figure = new PathFigure();
 
                    PointCollection bezierPoints = GetBezierPoints(pointCollection);
 
                    figure.StartPoint = bezierPoints[0];
                    for (int i = 1; i < bezierPoints.Count; i += 3)
                    {
                        figure.Segments.Add(new BezierSegment()
                            {
                                Point1 = bezierPoints[i],
                                Point2 = bezierPoints[i + 1],
                                Point3 = bezierPoints[i + 2]
                            });
                    }
 
                    geometry.Figures.Add(figure);
                    SplinePoints = geometry;
                }
                else
                {
                    SplinePoints = null;
                }
 
                Points = pointCollection;
            }
            else
            {
                Points = null;
                SplinePoints = null;
            }
        }
 
        #region Bezier Curve Building
 
        /*
         * Formulas and code pulled from Kerem Kat's MapBezier example:
         * http://www.codeproject.com/KB/silverlight/MapBezier.aspx
         */ 
 
        private PointCollection GetBezierPoints(PointCollection pts)
        {
            PointCollection ret = new PointCollection();
 
            for (int i = 0; i < pts.Count; i++)
            {
                // for first point append as is.
                if (i == 0)
                {
                    ret.Add(pts[0]);
                    continue;
                }
 
                // for each point except first and last get B1, B2. next point. 
                // Last point do not have a next point.
                ret.Add(GetB1(pts, i - 1, SplineTension));
                ret.Add(GetB2(pts, i - 1, SplineTension));
                ret.Add(pts[i]);
            }
 
            return ret;
        }
 
        private Point GetB1(PointCollection pts, int i, double a)
        {
            Point  derivedPoint = GetDerivative(pts, i, a);
            return new Point(pts[i].X + derivedPoint.X / 3, pts[i].Y + derivedPoint.Y / 3);
        }
 
        private Point GetB2(PointCollection pts, int i, double a)
        {
            Point derivedPoint = GetDerivative(pts, i+1, a);
            return new Point(pts[i + 1].X - derivedPoint.X / 3, pts[i + 1].Y - derivedPoint.Y / 3);
        }
 
        private Point GetDerivative(PointCollection pts, int i, double a)
        {
            if (pts.Count < 2)
                throw new ArgumentOutOfRangeException("pts", "Data must contain at least two points.");
  
            if (i == 0)
            {
                // First point.
                return new Point((pts[1].X - pts[0].X) / a, (pts[1].Y - pts[0].Y) / a);
            }
            if (i == pts.Count - 1)
            {
                // Last point.
                return new Point((pts[i].X - pts[i - 1].X) / a, (pts[i].Y - pts[i - 1].Y) / a);
            }
  
            return new Point((pts[i + 1].X - pts[i - 1].X) / a, (pts[i + 1].Y - pts[i - 1].Y) / a);
        }
        #endregion
    }
}
 
#endif



Note that I borrowed my Spline/Bezier code from Kerem Kat's example: http://www.codeproject.com/KB/silverlight/MapBezier.aspx

Essentially what it does, is takes prior points to adjust such that they curve into the next. The UpdateShapeFromPoints method was changed from the standard LineSeries and uses all of the helper methods that were added to calculate a Bezier curve.

We're pretty much almost done, except for one more minor touch. We need to have the default style included, such that it complies with how Silverlight Toolkit handles it's control templating. We add the following chunk of code to Themes\generic.xaml of the same project:


    <!--  charting:SplineSeries  -->
    <Style TargetType="charting:SplineSeries">
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="PathStyle">
            <Setter.Value>
                <Style TargetType="Path">
                    <Setter Property="StrokeThickness" Value="2" />
                    <Setter Property="StrokeMiterLimit" Value="1" />
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:SplineSeries">
                    <Canvas x:Name="PlotArea">
                        <Path Data="{TemplateBinding SplinePoints}" Stroke="{TemplateBinding Background}" Style="{TemplateBinding PathStyle}"/>
                        <Polyline Points="{TemplateBinding Points}" />
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Now, just recompile the Toolkit and you should be good to go!

I've included a "patch" with these files and their updates -- they should be capable of being pasted over their old versions (the Themes\generic.xaml just has the SplineSeries.xaml pasted into it -- I would strongly recommend you doing this with your own source):

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B9hGvs6yJgfJNzA4YmE1OTctNzNmYy00MzMzLWJhNDQtNTc0M2FlNTA3NzFm&hl=en_US

Tuesday, 12 July 2011

SilverlightToolkit Development/Building Guidance

If you're like me and want to alter some of Silverlight Toolkit's behavior (or add more functionality to it), chances are your builds will not work perfectly and you'll be in a constant state of mismatched assemblies.

I've noticed that Silverlight Toolkit has some major issues with the entire build process -- only loading different components and making references to other DLL's (which get deleted if you perform a clean build), not other projects. The second those are removed from the Binaries directory, the entire system goes to hell and back, eventually grabbing references to your DLL's in the Silverlight Toolkit install directory (somewhere in your root drive under Program Files, etc). What a pain! Then you'd have to go back and rebuild the references and re-specify their location. There has to be a better way, right?

Kind of.

What I ended up doing, and I highly recommend this for anyone working with Silverlight Toolkit on a regular basis -- is to build a new solution, rather than use the existing ones. Don't worry about including their Design or Testing projects, unless they're important to you. Then follow this process:

1. Build a new solution and include all relevant projects.

As stated before, place everything that you actually care about building. In my case, I was only concerned about the actual controls - not the testing or design. Since I'm already heavily involved with using the controls, I'll know if they're broken in test or design scenarios.
2. (Optional) Rename all projects to their full assembly name, rather than a shorthand version.

When viewing their projects, you'll notice things like Controls.Data or System.Controls, which are all very misleading. Everything falls under System.Windows.*. For sake of clarity, I found myself renaming them, so when I would eventually add references back to other projects, I could see the full name of the project/assembly built.
3. Change build path in each project, for all build-types, to a single directory.

You'd be surprised how many different places the assemblies for Silverlight Toolkit get built to. Each project seems to be written by someone else, with their own styles/paths for where assemblies that are built get stored. I simplified everything and made everything build to the Binaries folder -- always.
4. Referenced assemblies from other SDKs should be placed in a special folder under the build path.

This is a pattern we have at work -- if you build something that requires a reference to an SDK other than our own, you drop the assembly in a References project and store everything in source control for the entire team to use. You won't have to install the toolkits for other team members at this point, since they'll be in a standard location. For Design and Testing projects, SilverlightToolkit uses other SDKs, so this is vital for those of you wishing to build these.
5. Remove all references to SilverlightToolkit projects and re-add them as a reference to the projects in your solution.

As stated before, the references all get built to different places, which can cause for some crazy issues when you perform a clean. They then default back to your SilverlightToolkit install. It's horribly complicated. Referencing your projects will create appropriate dependencies that cause everything to build correctly and together. You can imagine what will happen to System.Windows.Controls.DataVisualizations when it tries to reference System.Windows.Controls and the assemblies are built at different times with different dependencies. If you can't, imagine shear sacrilege. And pain. And suffering.
If you follow these guidelines, you should have a much more stable/clean build of SilverlightToolkit, which will make future developments and enhancements easier to bake in (such as targeting a WPF platform for Charting). Unfortunately SilverlightToolkit also no longer accepts user contributions, due to licensing issues, and Microsoft is the sole development team. This makes getting features added a little (read: very) difficult.

Good luck and happy building!

Thursday, 7 July 2011

Embedding (Silverlight) Content in SharePoint Webparts

I'm going to start this off by saying that I am, by no means, a SharePoint expert. Developing for SharePoint is painful, but also rewarding. When it works, you feel an immense amount of pride that your solution worked after all of your labor. However, there's the immeasurable pain and headaches that come from attempting to write it.

That being said, I did find a very clever way of embedding a piece of content (in my case, a Silverlight xap -- but this could be anything, a JPEG, PNG, MP3, etc) into a SharePoint webpart. Given my background of making everything work on multiple platforms, I wanted to have the simplest possible way of doing this, without having to continually go back and forth with new code. Given I'm also trying to write a platform of tools and controls, this enables me to make a standardized package where we're not going to have conflicts with different versions of a chunk of content in the layouts folder with different assemblies/features installed.

The jist of this is that we're going to have ASP.NET share the content through a compiled DLL. Seems complicated? Not as much as you'd think. It's a relatively simple process:

  1. Add content to your project.
  2. Add information about the resource to your assembly.
  3. Utilize the resource.
First things first -- make sure you have a SharePoint project setup in Visual Studio 2010. I imagine this will probably work in prior versions of Visual Studio, but I haven't tested it.

1. Add content to your project.

Originally, I wanted to do things the "right" way for embedding my content -- I wanted to add each file into the mapped Layouts folder that Visual Studio 2010 gracefully provides. However, a minor issue arose from this -- my content is changing dynamically, and I needed to add it as a linked item (like the way I've done with Silverlight and WPF controls). Since the file is linked and not actually in the folder, Visual Studio doesn't have it in the directory and thus, it is not built. You could potentially make a script to copy it back/forth after the content is built (in my case, it's a Silverlight xap), but then you have issues with checking it out, etc.

Instead, we want to link it, since that's much, much, much easier. It prevents a lot of issues that could also result with conflicting and different features, in the event of any major issues with deployment.

So, I created a folder in my project called "Resources" where I would link this in. In this example, I have a Silverlight application called "HelloWorld.xap" and will be linking in the existing xap from the Release directory. Make sure that this is included as an "Embedded Resource" from the "Build Actions."



Gotta love our Hello World applications!


Now, that we have that done, we can get to the slightly uglier part.

2. Add information about the resource to your assembly.

In the SharePoint project, we need to open up AssemblyInfo.cs under Properties and edit it very minorly. We want to add in the new resource we have. I had to add in a reference to System.Web, and you likely will too.

At the bottom of the file, you will need to add one entry per resource, as follows:


[assembly: System.Web.UI.WebResource("EmbeddedSPContent.Resources.HelloWorld.xap", "application/x-silverlight-2")]

The first parameter of System.Web.UI.WebResource is the name of the file and its location in the project. Notice that to notate paths/directories, we need to use periods instead of slashes.

The second parameter of System.Web.UI.WebResource is the mime-type. What do you want the DLL to tell the browser that the file is? In my case, I want it to be recognized as a Silverlight application. This could potentially be a PNG, MP3, and so forth.

Still hanging on there? Cool, because we're almost done.



3. Utilize the resource.



Ah, yes -- the most important part. We have to actually use it now. But that's simple. I'm going to build a pretty standard WebPart and override RenderContents.


I added the following code to render it (a very quick/dirty Silverlight HTML mockup):


string resourceUrl = Page.ClientScript.GetWebResourceUrl(GetType(), "EmbeddedSPContent.Resources.HelloWorld.xap");
 
            string control = @"
            <div id=""silverlightControlHost"">
                <object data=""data:application/x-silverlight-2,"" type=""application/x-silverlight-2"" width=""400px"" height=""400px"">
                    <param name=""source"" value=""{MY_URL}"" />
                    <param name=""minRuntimeVersion"" value=""4.0.50826"" />
 
                    <a href=""http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=4.0.50826.0"" style=""text-decoration:none"">
                        <img src=""http://go.microsoft.com/fwlink/?LinkId=108181"" alt=""Get Microsoft Silverlight"" style=""border-style:none"" />
                    </a>
                </object>
            </div>";
 
            control = control.Replace("{MY_URL}", resourceUrl);
 
            writer.Write(control);


and look at the below, it works!



I know what you're thinking -- that's a sexy web part.

The sample is included on MSDN for others to use, so this will hopefully make it easier to follow what was done:

http://code.msdn.microsoft.com/Embedding-Resources-for-d61b251d

Please note that in order to get this working, we have to deploy it as a Farm Solution, rather than a Sandboxed Solution.

Saturday, 2 July 2011

Difference Between Firefox 4 and Firefox 5

Firefox is the second most widely used web browser in the world. It is used by thirty-percent of browser users worldwide. Firefox 4 was released on March 22, 2011 with major improvement over Firefox 3.6. But few weeks later Mozilla announced the release of Firefox 5 due to their new rapid release cycle (much like Google’s), and it was released in June, 2011. Although, Firefox 4 included major changes compared to its previous version, most expert reviewers agree that Firefox 5 and Firefox 4 are very similar and the changes seen on Firefox 5 do not merit a full version number.

Firefox 4

Firefox 4 was a major improvement over its earlier edition. Firefox 4 adds improved support for HTML5, CSS3, WebM and WebGL, by utilizing the power of Gecko 2.0 engine. A new JavaScript engine called JägerMonkey is included. The primary goals for the version 4 of this already impressive browser were improvements in performance, standards support, and user interface. Firefox 4 introduced a new and improved user interface to make it faster. A feature called Firefox Panorama allows the user to organize tabs into windows called groups and apply same operation on all tabs in a group. By default, tabs are now on top of the page, almost exactly similar to Chrome. Stop, Reload and Go buttons have been combined in to a single button, which changes state according to the current state of the page.

An audio API has been introduced in Firefox 4, which allows programmatically accessing or creating audio data associated with HTML5 audio element. This feature can be used to visualize, filter or show audio spectrums. Firefox 4 now offers a consistent layout/shaping across different operating systems. Other notable features are doorhanger notifications, application tabs and support for multitouch displays.

Firefox 5

Firefox 5 was released in June 21, 2011. Because Firefox 5 was released in a very short time (just after 3 months) after Firefox 4’s release date, there are no major changes to the GUI. But there are plenty of minor additions, improvements and bug fixes, that are claimed to help Firefox 5 relatively more secure, stable and usable. Along with all the new features introduced in Firefox 4, there is a new “Do Not Track” button in the preference menu in Firefox 5, which makes it very convenient to opt-out from advertisement companies that track the web history to display customized ads. In fact, Firefox 5 for Android is the first mobile browser with this feature. Firefox 5 includes a channel switcher to switch between the beta version and other test versions. Firefox 5 adds support for CSS animations. It includes better Linux support as well. Furthermore, they say Firefox 5 has better support for HTML5, XHR, SMIL, CSS3 and Math ML.

What’s the difference between Firefox 4 and Firefox 5

Although, Firefox 4 introduced lots of impressive features over its earlier release, Firefox 5 only adds minor improvements and few new features. Visually, Firefox 5 looks almost the same as Firefox 4. But, Firefox 5 is said to be more secure and more stable than Firefox 4. Two such notable features are the “Do Not Track” button and channel switcher in Firefox 5. Firefox 5 actually makes the “Do Not Track” header preference more visible and easily reachable. It is estimated that Firefox 5 provides improved memory, JavaScript, canvas and networking performance compared to Firefox 4. Firefox 5’s HTTP idle connection logic is better tuned compared to Firefox 4. Firefox 5 includes improved spell checking for more locales than Firefox 4. Mozilla claims that Firefox 5 provides dramatic performance improvement for its Linux users. Furthermore, Firefox 5 offers better support for web standards such as HTML5 and CSS3.

Mozilla claim that there are over thousand improvements in Firefox 5 (but most of them are bug fixes related to crashes, fonts, etc.). Therefore, Firefox 5 is claimed to be more stable than its predecessor. Firefox developers have improved the WebGL security by not allowing Firefox 5 to load cross-domain textures. To improve the performance of tabs, Firefox 5’s both setInterval and setTimeout have been set to 100 Milliseconds. According to several software critics that reviewed Firefox 5 after its release, although changes in other areas in Firefox 5 are minor, in terms of improvements in the areas of security, stability and usability, Firefox 5 is well worth the upgrade.



VisualTreeHelper

Ever noticed that WPF and Silverlight don't like to play nicely with their implementations of the same controls? Often, this is because they are derived differently. They both ultimately derive from DependencyObject, however the

In any case, the VisualTreeHelper is your best friend when it comes to navigating the structure of your controls. Essentially, it allows you to navigate down from a parent element, locating children. You can be a little recursive and then use the VisualTreeHelper on the child controls and dig deeper and deeper.

I've written a little helper that I've injected into the base class of all controls I'm writing for Silverlight and WPF.

/// <summary>
/// Navigates the control tree to find a child of a specific type.
/// </summary>
/// <param name="element">The parent element to search under.</param>
/// <param name="type">The type to search for.</param>
/// <returns>The first child of the specified type on success or null on failure.</returns>
protected object GetChildOfType(FrameworkElement element, Type type)
{
    int count = VisualTreeHelper.GetChildrenCount(element);
 
    for (int i = 0; i < count; i++)
    {
        FrameworkElement child = (FrameworkElement)VisualTreeHelper.GetChild(element, i);
 
        if (child != null)
        {
            if (child.GetType() == type)
                return child;
 
            object deeperChild = GetChildOfType(child, type);
 
            if (deeperChild != null)
                return deeperChild;
        }
    }
 
    return null;
}


Enjoy!

Wednesday, 22 June 2011

The XAML Message From Hell: "Error HRESULT E_FAIL has been returned from a call to a COM component."

The most irritating thing in XAML-based projects (Silverlight, WPF) is the infamous "Error HRESULT E_FAIL has been returned from a call to a COM component."

Why Microsoft didn't decide to catch it and give a more appropriate exception escapes me. However, some things I've learned about this:

Your XAML is failing to load!


Now, your XAML may actually be correct, is the funny thing -- but maybe your project is missing the references. For example, I'm using the Silverlight Toolkit and setting styles at runtime on the fly, allowing for potential theming. I kept getting this error, even though my other projects which use the same code are building fine -- so why is my latest project failing to build?

I missed a reference that was used in the other project. When it came to load the XAML style, it was likely getting hung up on the missing reference and saying, "OK, I see you want to use System.Windows.Controls.Layout.Toolkit... but what in the world is that?"

To rectify this issue, it was necessary to add references all over again.

So, my troubleshooting advice is:


  1. Make sure the XAML is correct. When writing cross-platform (i.e. Silverlight and WPF), some of the controls/targets behave differently or have different properties. Visual Studio generally points this out as a build error.
  2. Make sure that you are referencing all dependent assemblies. Due to Silverlight's dependencies, if I write a library on top of Silverlight Toolkit, which then gets used in an application, that application needs to have the references to Silverlight Toolkit.

Good luck and when in doubt -- get an extra set of eyes!

Saturday, 18 June 2011

Silverlight and WPF Measurement Pain

One of the more irritating things I've been dealing with in Silverlight and WPF is that sizing and automatic content placement is a pain. Don't get me wrong -- I love the new rendering engine and how it works to update the layout, but it can be a pain to "think in terms of WPF or Silverlight." Especially when the two don't exactly have the same methods of working.

I made a decision fairly early on to build a static Utilities class, which holds all of my ugly code where I have to use the very, very, very fun

#if SILVERLIGHT            

#else

#endif

constructs, which gets me to the point of this post.

It's difficult to determine the size of a control -- especially the size it wants to be. Ideally, yes, the DesiredSize will have that exact size. Unfortunately, the DesiredSize is somewhat misleading -- it's the DesiredSize of the space it's confined to. In addition, it works totally different between Silverlight and WPF.

I was primarily concerned with sizing things before they are rendered (they have no parent). This is drastically different from figuring out a size once they are in the layout tree and the control itself has a parent.

In any case, I'm only going to cover the basics. The most simple way to determine the size something wants to take up is to do artificially measure the element and then check the desired size. Assuming I have a TextBlock named Frank, the following code would determine the size Frank really wants:

Frank.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Frank.Height = Frank.DesiredSize.Height;
Frank.Width = Frank.DesiredSize.Width;

Ah, a crafty trick (and it works in both Silverlight and WPF without a hitch; if Frank didn't have a parent, the code would be different between the two platforms). We make the control believe it has unlimited space, and then we can use DesiredSize. Play with it a little bit -- set a breakpoint on DesiredSize before and after this call and see the difference.

For more information about how the Layout system works (which is an excellent read for understanding how WPF/Silverlight work), check out this MSDN article: http://msdn.microsoft.com/en-us/library/ms745058.aspx

Wednesday, 8 June 2011

Change logical and physical file names of SQL Server database

The easy way of renaming a SQL Server database is to open SQL Server Management Studio, select the database name on the tree on the left handside, hit F2 and type in the new name you want. It’s like renaming a file in a folder. But when you do that, the logical name and physical filenames of the database won’t change. In order to verify this, right click on the database name, select properties. Select Files sction on the left hand side. You can see the logical name and file name for data and log files on the right hand side.
Before attempting to change the names, you should take the database offline. You can do that using SQL Management Studio (right click the database and select Tasks > Take Offiline) or using the following SQL steatement.
ALTER DATABASE database_name SET OFFLINE
In order to change the logical name, execute the following T-SQL statement in a new query window for the database in SQL Server Management Studio. You can use the same statement for both data and log files by changing current_logical_name and new_logical_name.
ALTER DATABASE database_name MODIFY FILE (NAME=“current_logical_name”, NEWNAME=“new_logical_name”)
In order to change the physical file names, execute the following T-SQL statement. You can use the same statement for both data and log files by changing the logical_name and new_file_name.
ALTER DATABASE database name MODIFY FILE (NAME=“logical_name”, FILENAME=“C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\new_file_name_you_want.mdf”)
The data and log file location may change depending upon your SQL Server installation.
After executing the SQL statements and before bringing back the database online, go ahead and change the file names for data and log files (.mdf and .ldf) to new_file_name in the file system. Now you are good to go. Go ahead and bring the database online.
You can do that using SQL Management Studio (right click the database and select Tasks > Bring Online) or using the following SQL steatement.
ALTER DATABASE database_name SET ONLINE

Tuesday, 24 May 2011

SQL Server function code for formatting numbers to have Comma seperator with Out any Loop

Hi,


SQL Server does't have any inbuild function to put thousand Comma sepeator. You can use the Below MS SQL server user function or Code for formatting Numeric Values to have Comma seperator.

This Approach is an optimal one and dont use any Loop.

View Alternate Approach 


CREATE
FUNCTION [fn_CommaSeperateNumeral]

(@completeValue NVARCHAR(200)

)
RETURNSNVARCHAR(200)
AS

BEGIN
DECLARE @intValue NVARCHAR(50), @precValue NVARCHAR(50)

SELECT @intValue = SUBSTRING(CONVERT(NVARCHAR,CONVERT(MONEY,@completeValue),1), 1, CHARINDEX('.',CONVERT(NVARCHAR,CONVERT(MONEY,@completeValue),1),1)-1)

IF CHARINDEX('.',@completeValue,1) >0

SELECT @precValue = SUBSTRING(@completeValue, CHARINDEX('.',@completeValue,1)+1,LEN(@completeValue))

SELECT @completeValue = @intValue

IF @precValue IS NOT NULL

SELECT @completeValue = @completeValue + '.' + ISNULL(@precValue,'')

RETURN @completeValue

END

Saturday, 21 May 2011

Using the “Copy XAML” Feature in Expression Design to Create HTML5 SVG Path Data

Introduction

One of the things that I recently had someone show me was using the “Copy XAML” feature in Expression Design to create HTML5 SVG Path Data. I later found out that this was demoed at MIX11 in a session called HTML5 for Silverlight Developers. So with that said, I don’t take credit for discovering this, just documenting it for others to use.
If you want to see the final product, then click here. Go ahead and right click on the page and you will see it’s just path data (no image – just straight up HTML).
Let’s get started:
  1. Fire up Microsoft Expression Design 4.
  2. Create a New Document (Hitting CTRL-N).
  3. Draw something cool. (Yes, I’m a big fan of the Dark Knight.)
  4. Hit CTRL-A to select everything.
  5. Click Edit-> Copy XAML.
  6. Go ahead and open something like Notepad2 and paste what is in your clipboard.
  7. You now want to clean this up a bit. The only parts we are interested in keeping is the path data starting with M as shown below. Go ahead and remove everything else in Red.
  8. And ending with the Z from the Path Statement. Again, remove the part in Red below.
  9. Now that we have one continuous line of points, let’s drop that into the path of a HTML5 SVG as shown below:
    <!DOCTYPE html>
    <html lang="en">
     
    <head>
        <title>Expression Web and HTML5</title>
    </head>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg">
     
        <path fill="black" d="M 218.806,168.146C 218.02,167.674 
        217.049,168.699 216.14,168.812C 215.258,168.922 214.362,168.812 
        213.473,168.812C 208.585,168.812 203.667,169.352 198.808,168.812C 
        184.373,167.208 168.511,159.798 160.813,147.482C 152.479,134.146 145.963,119.41 
        142.149,104.154C 141.28,100.678 141.685,96.9641 140.816,93.4883C 
        140.594,92.5995 140.263,91.731 140.149,90.822C 140.039,89.9401 140.259,89.0376 
        140.149,88.1556C 139.766,85.0927 138.546,79.7737 135.483,80.1566C 
        131.418,80.6648 129.048,85.2586 126.151,88.1556C 121.053,93.254 117.974,100.039 
        114.152,106.153C 106.638,118.177 97.8295,129.467 91.4886,142.149C 86.3243,
        152.477 86.098,164.668 84.8227,176.145C 83.2401,190.388 81.0727,204.563 
        79.4901,218.806C 77.0578,240.697 80.0911,262.943 82.823,284.798C 85.246,
        304.182 98.7341,323.407 115.486,333.458C 123.447,338.235 132.674,340.521 
        141.482,343.457C 144.959,344.616 148.511,345.669 152.148,346.123C 153.057,
        346.237 153.905,346.676 154.814,346.79C 155.696,346.9 156.786,347.345 
        157.48,346.79C 158.196,346.218 158.261,345.033 158.147,344.124C 158.024,
        343.138 157.163,342.388 156.814,341.457C 155.727,338.56 154.567,335.689 
        153.481,332.792C 150.491,324.818 148.88,316.39 146.815,308.128C 140.451,
        282.673 138.927,248.691 157.48,230.138C 169.285,218.333 194.244,210.479 
        206.807,221.472C 214.962,228.607 219.511,239.624 222.139,250.135C 223.028,
        253.69 223.916,257.246 224.805,260.801C 225.021,261.663 224.695,262.585 
        224.805,263.467C 224.919,264.376 224.686,265.662 225.472,266.133C 226.257,
        266.605 227.434,266.053 228.138,265.467C 228.902,264.831 229.027,263.689 
        229.471,262.8C 230.763,260.217 231.79,257.506 232.804,254.801C 233.194,
        253.761 234.137,253.024 234.804,252.135C 235.471,251.246 236.307,250.462 
        236.804,249.469C 238.983,245.11 239.971,240.103 242.803,236.137C 246.394,
        231.11 251.942,227.568 257.468,224.805C 261.912,222.583 267.313,223.344 
        272.133,222.139C 277.346,220.835 282.917,218.836 288.131,220.139C 292.951,
        221.344 298.229,223.033 301.462,226.805C 305.762,231.821 306.039,239.202 
        308.128,245.469C 312.152,257.542 314.549,270.171 316.127,282.798C 317.408,
        293.047 322.499,302.456 326.126,312.128C 330.006,322.474 332.112,333.404 
        334.791,344.124C 335.952,348.767 338.963,352.812 340.124,357.455C 342.804,
        368.175 346.393,378.665 348.79,389.451C 349.812,394.053 350.205,398.773 
        350.789,403.45C 350.9,404.331 350.679,405.234 350.789,406.116C 350.903,
        407.025 350.54,408.782 351.456,408.782C 354.204,408.782 352.86,
        403.466 353.456,400.783C 
        354.425,396.424 354.789,391.918 354.789,387.452C 354.789,374.779 353.456,
        362.129 353.456,349.456C 353.456,318.713 349.327,286.254 360.122,257.468C 
        361.866,252.816 365.043,248.788 366.787,244.136C 368.183,240.414 369.102,
        236.058 372.12,233.471C 378.307,228.168 386.212,224.782 394.117,222.805C 
        402.958,220.595 414.528,217.542 421.447,223.472C 425.528,226.97 427.264,
        232.559 429.446,237.47C 432.051,243.331 433.317,249.771 434.112,256.135C 
        434.461,258.924 433.968,264.134 436.779,264.134C 437.772,264.134 437.871,
        262.431 438.112,261.467C 438.327,260.605 438.112,259.69 438.112,258.801C 
        438.112,256.135 438.112,253.468 438.112,250.802C 438.112,242.754 437.952,
        234.341 440.778,226.805C 442.877,221.207 448.31,216.257 454.11,214.806C 
        467.435,211.475 482.765,214.802 494.771,221.472C 499.469,224.082 505.646,
        225.098 508.77,229.471C 513.223,235.706 513.234,244.13 515.436,251.469C 
        517.051,256.853 519.405,262.013 520.768,267.467C 522.93,276.113 520.93,
        285.483 518.768,294.13C 516.939,301.449 518.346,310.399 513.436,316.127C 
        510.321,319.761 504.586,319.779 500.104,321.46C 495.004,323.372 489.511,
        324.117 484.106,324.793C 483.224,324.903 482.329,324.793 481.44,324.793C 
        480.551,324.793 479.513,324.3 478.773,324.793C 477.947,325.344 478.143,
        326.756 477.44,327.459C 476.738,328.162 474.774,327.799 474.774,328.792C 
        474.774,329.903 476.498,330.203 477.44,330.792C 478.283,331.319 479.176,
        331.776 480.107,332.125C 484.194,333.658 488.56,334.309 492.772,335.458C 
        502.534,338.121 512.675,339.403 522.768,340.124C 538.339,341.236 553.853,
        345.162 569.429,344.124C 578.687,343.506 590.053,343.17 596.092,336.125C 
        605.017,325.713 614.528,315.766 622.755,304.795C 631.916,292.582 636.108,
        277.037 639.42,262.134C 648.329,222.042 638.769,177.679 622.089,140.149C 
        618.044,131.048 610.467,123.861 603.425,116.819C 585.079,98.4733 559.059,
        89.2664 534.766,80.1566C 522.464,75.543 510.497,68.9426 497.438,67.4916C 
        487.719,66.4116 476.855,63.7848 468.108,68.1581C 462.583,70.9209 457.542,
        76.693 456.776,82.823C 456.264,86.9241 461.186,89.8992 464.109,92.8217C 
        470.886,99.5995 478.355,105.818 484.106,113.486C 489.533,120.722 495.649,
        128.508 496.771,137.483C 497.674,144.705 494.661,151.999 492.105,158.814C 
        488.813,167.593 483.392,176.508 475.441,181.477C 467.57,186.397 457.392,
        186.81 448.111,186.81C 437.214,186.81 426.278,186.68 415.448,185.477C 
        408.514,184.706 401.553,183.836 394.784,182.144C 389.259,180.763 381.333,
        181.239 378.786,176.145C 377.46,173.493 377.724,170.291 376.786,167.479C 
        371.339,151.138 370.787,133.378 370.787,116.152C 370.787,109.931 370.787,
        103.709 370.787,97.4878C 370.787,94.8215 370.787,92.1552 370.787,89.4888C 
        370.787,88.6 371.649,87.0381 370.787,86.8225C 369.709,86.553 368.672,
        87.8577 368.121,88.8223C 367.68,89.5939 368.336,90.6263 368.121,91.4885C 
        367.88,92.4526 367.136,93.2245 366.787,94.1549C 366.466,95.0127 366.343,
        95.9324 366.121,96.8212C 364.722,102.418 364.035,108.326 361.455,113.486C 
        357.646,121.103 345.242,121.208 336.791,120.152C 328.631,119.132 322.813,
        111.064 317.46,104.82C 312.957,99.566 307.13,95.3571 303.462,89.4888C 
        301.972,87.1055 301.144,84.2787 300.796,81.4898C 300.686,80.6079 300.906,
        79.7054 300.796,78.8235C 300.682,77.9144 301.045,76.1572 300.129,76.1572C 
        299.018,76.1572 298.718,77.8813 298.129,78.8235C 297.603,79.6661 297.145,
        80.5594 296.796,81.4898C 295.434,85.1232 295.071,89.0571 294.13,92.8217C 
        291.377,103.834 293.384,115.535 292.13,126.817C 290.754,139.202 287.593,
        153.371 278.132,161.48C 270.644,167.898 260.036,169.349 250.802,172.812C 
        244.917,175.018 238.422,175.478 232.138,175.478C 228.521,175.478 224.416,
        175.581 221.472,173.478C 220.568,172.833 219.969,171.806 219.473,170.812C 
        219.063,169.993 219.592,168.617 218.806,168.146 Z" />
        
        </svg>
    </body>
    </html>
  10. Now load the page into a browser and you will get the following. You can test this in a browser by going here:

Conclusion


That was pretty easy to do and only took about 5 minutes. There are a lot of things you can do with path data as sometimes it is easier to drop in path data instead of add an image to a project. I hope you found this post helpful.