Wednesday, December 5, 2012

Silverlight initalization error

So I was copying and pasting some classes into my Silverlight project and when I ran it gave me a pop-up window saying it couldn't initialize the Silverlight app.

A cursory look revealed that I had accidentally deleted the "ClientBin" folder from my Silverlight web project (remember that there is the client piece and the web piece on SL apps).  I added the folder back but the XAP file wasn't there!

I did a search and didn't find the solution for this but I figured it out so I wanted to make it available.

You'll need to go into the project properties (right click on the .WEB project and choose properties).  Under the Silverlight Applications tab you will need to add the application.

Click "Add" then choose "use an existing Silverlight project in the solution" and add the SL project into the project.

As part of this it will also recreate the ASPX and HTML page(s) if you accidentally deleted those as well.

The exact message from the browser is: "Error: Unhandled Error in Silverlight Application
Code: 2104     Category: InitializeError  Message: Could not download the Silverlight application. Check web server settings".

Wednesday, November 28, 2012

Silverlight Binding Error

I ran into an error in Silverlight (v4) today and thought I might take a moment to document the problem in case anyone runs across the same issue.

I created a data grid and was assigning the columns manually.  I decided to leave one column without a binding because it will be a combo box field and I wasn't ready to tackle that yet.

When I ran my app I received and error when I bound my objects to the grid.  Since this is just a sandbox proof-of-concept thing I didn't have it wrapped with a try...catch so it actually came up as an unhandled exception in Internet Explorer.

The error will be "Value cannot be null."  And you might drill down and find an error about an unexpected value as well.

The problem is that you have a field without a binding.  So I removed this field (you may opt to add a binding to it) and problem solved.

Tuesday, September 25, 2012

Unable to install or run the application. The application requires...

Ran into an error today doing a Click Once style deployment on my new PC.  The end-user was getting an error ""Unable to install or run the application. The application requires ..." which proceeds to list the missing DLL and that it needs to be installed in the GAC.

I checked the reference and "Copy Local" was set to true so I knew the DLL was in the directory when the app runs locally but I found out that Click Once has a way of managing its dependency files.

When you right click on the project it brings up the project properties window.  In VS2010 under the "Publish" tab there is an "Application Files" button.  When you click this button it lists all the assemblies and gives you the option under the "Publish Status" column to choose if you want to include these files automatically in the deployment.

If you set to "Include" then its there but if its set to "Prerequisite" it will want that file to be installed on the machine prior to running the Click Once.  Exclude is also an option but I don't think it does the prerequisite check.

Wednesday, May 2, 2012

Detecting Version in C# / .NET

I've found occasion recently to know the version of Windows that a person is using in order to make a call to the correct API.  Mainly, this has been a concern with clients using Windows XP and now moving onto Windows 7 and finding compatibility issues with 3rd party components.

By using System.Environment.OSVersion.Version we can look at the version of Windows running and determine the best source of action.  The two properties we are concerned with are "Major" and "Minor".

You can find a complete list by searching for "System.Environment.OSVersion.Version" but basically, here is the rundown:

Major 5 will be 2000, XP, or 2003.  You can tell by the minor version which one it is.  Window 2000 is 0, XP is 1, and 2003 is 2.

Major 6 will be Vista, 2008, or 7.  Minor 0 is Vista, 2008 is 1, and Windows 7 is 2.

There is also a "Platform" property that will tell you if the machine is running Windows or Unix or WinCE, etc.  Since I know my client is running Windows XP or 7 ...  I don't concern my self with that but you might need to.

So a quick example... maybe I need to load a different form with a Windows XP or Windows 7 specific control.  I could do something like this:

If (System.Environment.OSVersion.Version.Major == 6)
{
     //We're running Vista, 2008, or 7
     //Load a form with the Windows 7 component
}
else
{
     //We're running 2000, XP, or 2003.  Hopefully not still on NT or older!!!
     //Load a form with old Windows components
}

Thursday, February 23, 2012

Database Connectivity in .NET

Something to keep in mind about database connectivity in .NET is to always ensure your connection is being disposed of properly.

Many times as developers we code according to the "happy path" which can lead to some undesirable behaviors when an unhappy path occurs.

Here is a quick example (excuse any typos as I'm doing this from memory):

SqlConnection cnn = new SqlConnection(myConnectionString);
cnn.Open();
SqlDataReader reader = cnn.ExecuteReader(someQueryOrCommand);
cnn.Close();

We open a connection, get some data, and we close the connection.  Easy, right?  Probably works 99% of the time with no problems.

What happens if we timeout during the execution of our SQL query or command?  Most of us are/should be using a try...catch block to catch that exception and deal with that error instead of having the app crash due to an un-handled exception.

A potentially big problem exists regardless of the try...catch block.  Each time this code is executed and fails for whatever reason we are leaving an orphaned connection.  If this happens enough times, we run out of available connections on the server and SQL is not responsive.

Commonly I see this problem when the database is performing slowly and causing a lot of database calls to timeout.  A user may repeatedly retry a piece of functionality or may repeatedly close and re-open the application trying to get it to run.  These orphan connections typically make the problem worse to the point that the server has to be rebooted.

A good way to handle/prevent trashing your available connection is by using a "finally" block in addition to the try...catch.

The finally block will execute regardless if the outcome of the code (IE, regardless if it goes through the try code with no problems or it hits the exception code block).

Pseudocode for this solution looks like this:

SqlConnection cnn = null;

try
{

cnn = new SqlConnection(myConnectionString);
cnn.Open();
SqlDataReader reader = cnn.ExecuteReader(someQueryOrCommand);

}
catch (Exception ex)
{
// do something here
}
finally
{
if (cnn != null && cnn.State == ConnectionState.Open)
    cnn.Close();
}

Thursday, February 16, 2012

Missing .NET Functionality

Something I've noticed recently in .NET4 is that certain functionality doesn't seem to be available.


Consider the following line:

string value = ConfigurationSettings.AppSettings["somevalue"];

I get a warning in the compiler "Warning 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: "This method is obsolete, it has 
been replaced by 
System.Configuration!System.Configuration.ConfigurationManager.AppSettings"

However, when I type ConfigurationManager, nothing shows up in Intellisense!

The solution to this is to go to "Add reference" and add the reference to "System.Configuration" in your solution.  Now, that functionality is available.

This is also the case for the "HttpWebRequest" object location in System.Web.

I'm not sure why 4.0 requires this whereas it just worked in older versions of .NET but it is what it is.