Pages

Tuesday, October 6, 2009

Unable to cast object of type 'System.DBNull' to type 'System.String'

When trying to assign data from a datasource to string variables where some of the fields contained null values i was was getting the following error.

Unable to cast object of type 'System.DBNull' to type 'System.String'

I know this is really simple stuff but as a beginner it took me a while to figure this out. If you use the Convert.ToString method then even though your data may be the correct datatype to start with it will convert a null value to an empty string instead of throwing the above error.


MyStringVariable = Convert.ToString(myDataReader["MyDBFieldName"]);

Wednesday, September 30, 2009

About My Blog

I live in the UK and work for a medium sized publishing company. I have always enjoyed computers and have recently started developing web based applications using .net, c# and SQL server. Previously i have developed several applications in classic asp,visual basic 6 and MS Access. As the demand at work for data driven applications has increased, combined with the fact that nobody else in the world seems to be using classic asp anymore i thought it's about time i learned something new.

This blog is to discuss coding resources, websites, blogs, forums etc that i find useful. I may even publish some of my own techniques and tips along the way. Please let me know if you find any of my posts useful and feel free to correct me when i get it wrong.
 
 

Blogger