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
}
Wednesday, May 2, 2012
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();
}
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:
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.
Friday, November 18, 2011
The Power of (Kindle) Fire
I may do a video review of the Fire at some point because I'm tired of silly unboxing videos and people fumbling through the Kindle Fire UI making stupid comments. But for now ... here is my article on the Fire as requested by some friends of mine.
First off let's clear some things up. This device has been dubbed an iPad killer. In some ways (which we will discuss) it could be but in other ways its not. The truth is one is chicken and the other is beef. Some people prefer one, some people prefer the other. And hey, some people might prefer pork of seafood. [Man, I'm getting hungry.] No the Fire doesn't have a camera, 3G, microphone, GPS, or a pretentious price tag. It is a $199 device and an iPad is a $600 device. Personally, I think its a smart move too.
Your smart phone will have all the features I mentioned and already has a data plan attached to it. So for someone like me who is a "bang for the buck" individual I don't want to pay for duplicate features and a second data plan. For some people this won't be an issue or concern. Great! That's fine.
So what is the Kindle Fire? Its a device to consume Amazon content (magazines, books, music, movies, tv shows, etc), run apps, and do some web browsing. In that regard (except for using iTunes content) it sounds just like an iPad at 1/3 the cost.
What do I like about the Fire? It is not too bulky so you can use it with one hand. Content can be stored in the Cloud or on the device. Purchasing content is easy and there is good integration between the Amazon site on your PC and the Kindle device. For example, if I purchase content on Amazon it will be available on the Kindle to download or view.
What don't I like about the Fire? The "8GB" storage is actually closer to 6.5 with everything that is pre-loaded. Since you can store content on the Cloud that's not too big of a deal but if you're trying to load up on movies (less of an issue with books or music) then you might not have enough space. Allegedly you can store about 10 movies on the Kindle Fire. The plus side of this is that if you're connected to wireless you can pull content from your private cloud storage or stream movies.
What do I like about the Amazon content? The selection is there. Most new media is available for download. The pricing is slightly better (say $15 for a new popular movie) vs $20 for the physical media. There is also rental options that make content available for $1.99 - $5.99. Most TV shows are about $1.99 for an episode or the season can be purchased.
Music prices seem to be fairly consistent about $0.99 a song but album prices can vary. Obviously book prices vary along with magazine subscriptions.
With Prime you get free streaming of videos and some limited "loaner book" selections. The content isn't necessarily newer stuff but its not all junk. Where it really shines is the amount of kid friendly content (Sesame Street, etc).
What do I dislike about the Amazon content? I'd prefer newer content over quantity of content for the Prime stuff. As far as I know there is no "add-on" to download music for a flat rate like Zune Pass or others. Some of the content (like Amazon's audio books; Audibles) doesn't seem well integrated with Fire yet.
Apps are lacking as well. I can get some of the more popular apps (Angry Birds, Facebook, Pandora, etc) on my device but ones like ESPN Fantasy Football don't appear to exist.
Amazon Prime? For $80/yr which includes free 2-day shipping on Amazon orders I think Prime is one of the better deals going on. That is basically $6.66 (nice) per month and includes not only streaming content but the loaner books (1 per month). So better deal than Netflix, etc.
Is this device good for kids? Yes, if supervised. There is no filtering/parental controls for web browsing or content downloads. And since even free content requires having all your billing stuff setup there isn't much to prevent your child from running up a huge bill on content.
In the first night of using the Fire and trying to get my kid to bed I was able to read her several children's books and stream episodes of Sesame Street. The finale I bought Kung Fu Panda ($6.99) and was able to stream it instantly. I later downloaded it so we can watch it on the go.
Other thoughts? Kindle doesn't have much in the way of inputs. It has a mini stereo jack and a power plug. The included power cable is a wall cable so I'm not sure if you had a USB cable with the correct connector if you could access the Fire or not. (IE as a mounted drive.)
Since you're purchasing digital content (not tied to a device) it seems like you could watch the same content on a computer or connect a computer to your TV and watch it that way. That seems pretty neat to me because now you're not having to re-purchase media.
Again, no SD ports or anything but you can upload content on the Amazon site to their cloud so that is handy. I think you can even upload non-Amazon music content as well.
Web browsing experience is closer to PC than Smart Phones are. Wasn't overly fast but wasn't overly slow either.
Final verdict: Great device if you have modest needs and the price is very reasonable.
First off let's clear some things up. This device has been dubbed an iPad killer. In some ways (which we will discuss) it could be but in other ways its not. The truth is one is chicken and the other is beef. Some people prefer one, some people prefer the other. And hey, some people might prefer pork of seafood. [Man, I'm getting hungry.] No the Fire doesn't have a camera, 3G, microphone, GPS, or a pretentious price tag. It is a $199 device and an iPad is a $600 device. Personally, I think its a smart move too.
Your smart phone will have all the features I mentioned and already has a data plan attached to it. So for someone like me who is a "bang for the buck" individual I don't want to pay for duplicate features and a second data plan. For some people this won't be an issue or concern. Great! That's fine.
So what is the Kindle Fire? Its a device to consume Amazon content (magazines, books, music, movies, tv shows, etc), run apps, and do some web browsing. In that regard (except for using iTunes content) it sounds just like an iPad at 1/3 the cost.
What do I like about the Fire? It is not too bulky so you can use it with one hand. Content can be stored in the Cloud or on the device. Purchasing content is easy and there is good integration between the Amazon site on your PC and the Kindle device. For example, if I purchase content on Amazon it will be available on the Kindle to download or view.
What don't I like about the Fire? The "8GB" storage is actually closer to 6.5 with everything that is pre-loaded. Since you can store content on the Cloud that's not too big of a deal but if you're trying to load up on movies (less of an issue with books or music) then you might not have enough space. Allegedly you can store about 10 movies on the Kindle Fire. The plus side of this is that if you're connected to wireless you can pull content from your private cloud storage or stream movies.
What do I like about the Amazon content? The selection is there. Most new media is available for download. The pricing is slightly better (say $15 for a new popular movie) vs $20 for the physical media. There is also rental options that make content available for $1.99 - $5.99. Most TV shows are about $1.99 for an episode or the season can be purchased.
Music prices seem to be fairly consistent about $0.99 a song but album prices can vary. Obviously book prices vary along with magazine subscriptions.
With Prime you get free streaming of videos and some limited "loaner book" selections. The content isn't necessarily newer stuff but its not all junk. Where it really shines is the amount of kid friendly content (Sesame Street, etc).
What do I dislike about the Amazon content? I'd prefer newer content over quantity of content for the Prime stuff. As far as I know there is no "add-on" to download music for a flat rate like Zune Pass or others. Some of the content (like Amazon's audio books; Audibles) doesn't seem well integrated with Fire yet.
Apps are lacking as well. I can get some of the more popular apps (Angry Birds, Facebook, Pandora, etc) on my device but ones like ESPN Fantasy Football don't appear to exist.
Amazon Prime? For $80/yr which includes free 2-day shipping on Amazon orders I think Prime is one of the better deals going on. That is basically $6.66 (nice) per month and includes not only streaming content but the loaner books (1 per month). So better deal than Netflix, etc.
Is this device good for kids? Yes, if supervised. There is no filtering/parental controls for web browsing or content downloads. And since even free content requires having all your billing stuff setup there isn't much to prevent your child from running up a huge bill on content.
In the first night of using the Fire and trying to get my kid to bed I was able to read her several children's books and stream episodes of Sesame Street. The finale I bought Kung Fu Panda ($6.99) and was able to stream it instantly. I later downloaded it so we can watch it on the go.
Other thoughts? Kindle doesn't have much in the way of inputs. It has a mini stereo jack and a power plug. The included power cable is a wall cable so I'm not sure if you had a USB cable with the correct connector if you could access the Fire or not. (IE as a mounted drive.)
Since you're purchasing digital content (not tied to a device) it seems like you could watch the same content on a computer or connect a computer to your TV and watch it that way. That seems pretty neat to me because now you're not having to re-purchase media.
Again, no SD ports or anything but you can upload content on the Amazon site to their cloud so that is handy. I think you can even upload non-Amazon music content as well.
Web browsing experience is closer to PC than Smart Phones are. Wasn't overly fast but wasn't overly slow either.
Final verdict: Great device if you have modest needs and the price is very reasonable.
Friday, August 26, 2011
The type initializer for ... threw an exception
I was given some old code that I needed to port/partially rewrite. The code had some funky things like a class trying to reference a static class as its base (which C# doesn't allow). My guess is someone started to rewrite the code before they checked it into source control and never completed it.
Anyhow, I wrote a test app for this code after making a few tweaks and when this particular object was initialized it hit the declaration for the constructor and quit. For some reason, .NET didn't throw any exceptions; it just stopped executing the code.
I wrapped my call with a try catch and caught the following: "The type initializer for 'NameOfClassICalled' threw an exception". [This was a TypeInitializationException.]
After carefully stepping through the code I found my simple yet elusive mistake. The code was initializing the base class which has static methods it was trying to load. These static methods used the ConfigurationManager which uses the App.config... which I failed to include in my project.
Adding my app.config with the proper entries and I was off to the cases. Reminder: The parent assemblies config is the one that is used by assemblies called during execution.
Monday, August 22, 2011
Portable Storage Devices
Had a situation today where I needed to copy some files off a computer and didn't have a thumb drive handy. Solution, I ended up using my cell phone which could have its SD card mounted as a hard drive. This got me thinking and I put together a list of devices I could think of that can be used with your computer a hard drive aside from the typical.
1. Smart phones
2. MP3 players
3. Digital Camcorder
4. Digital Camera
Friday, August 19, 2011
SSL content
Ran into something the other day that I thought was worth mentioning.
If you have a site that is secured with SSL then pay close attention to where you are referencing graphics and other content from. If you have a logo, for example, on an unsecured site that you reference then users will get a nasty message about the secured page having unsecured content. This will happen each time the visit the site and possibly each page they visit.
If you have a site that is secured with SSL then pay close attention to where you are referencing graphics and other content from. If you have a logo, for example, on an unsecured site that you reference then users will get a nasty message about the secured page having unsecured content. This will happen each time the visit the site and possibly each page they visit.
Subscribe to:
Posts (Atom)
