Dan posted on January 15, 2010 12:09

Need to find the month name by its number?

Easy!

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i)


Posted in: Development  Tags:

Recently I've been developing a small application using VS2008 SP1, Entity Framework and SQL Server 2008.  The target server is running Windows Server 2003 and SQL Server 2005. 

When I came to deploy my application I scripted the database and data (because you can't use SQL 2008 backups in 2005) and deployed my application, time to run and what do I get:

"Type datetime2 is not a defined system type"

Interesting!!  I check my database compatibility which looks fine for the target server:

image

It looks like Entity Framework ignores the underlying compatibility level, really useful right.

So to fix it, open the model in an xml editor and change the ProviderManifestToken to your target database.

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
   3:   <!-- EF Runtime content -->
   4:   <edmx:Runtime>
   5:     <!-- SSDL content -->
   6:     <edmx:StorageModels>
   7:     <Schema Namespace="TcwsModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">

One thing to note is that if you update your model you'll need to update the above property again.

Why didn't I stick to NHibernate :-(


Posted in: Development  Tags:

I needed to parse a known string format (DDMMYYYY) into a DateTime object and as I started to write the code I thought it looked a bit nasty and there must be a better way:

   1: private DateTime ParseDate(string date)
   2: {
   3:     DBC.CheckPre("Date can't be null", !string.IsNullOrEmpty(date));
   4:     DBC.CheckPre("Date length should be 8 for importing (DDMMYYYY)", date.Length == 8);
   5:  
   6:     string dateToConvert = string.Format("{0}/{1}/{2}", date.Substring(0, 2), date.Substring(2, 2), date.Substring(4, 4));
   7:     DateTime convertedDate;
   8:  
   9:     if (!DateTime.TryParse(dateToConvert, out convertedDate))
  10:         throw new FormatException(string.Format("Unable to format date:{0}", date));
  11:  
  12:     return convertedDate;
  13: }

I'd seen DateTime.TryParseExact before but hadn't really looked into it until today, the MSDN documentation can be found at http://msdn.microsoft.com/en-us/library/h9b85w22.aspx.

I've used formatters such as:

return DateTime.Now.ToString("ddMMyyyy");

TryParseExact takes an argument of string formats allowing you easily parse a string like this:

   1: private DateTime ParseDate(string date)
   2: {
   3:     DBC.CheckPre("Date can't be null", !string.IsNullOrEmpty(date));
   4:     DBC.CheckPre("Date length should be 8 for importing (DDMMYYYY)", date.Length == 8);
   5:  
   6:     DateTime convertedDate;
   7:  
   8:     if (!DateTime.TryParseExact(date, "ddMMyyyy", new CultureInfo("en-GB"), DateTimeStyles.None, out convertedDate))
   9:         throw new FormatException(string.Format("Unable to format date:{0}", date));
  10:  
  11:     return convertedDate;
  12: }

Posted in: Development  Tags:

I came across a situation where by I needed to set a constructor argument to null in the Spring.Net container configuration file.  Took me a little digging around in the docs but found it eventually:

   1: <object id="AmlHandler" type="Project.AmlHandler, Project.Lib" autowire="constructor" lazy-init="true">
   2:   <constructor-arg name="nextHandler">
   3:     <null/>
   4:   </constructor-arg>
   5:   <constructor-arg name="checkProvider" ref="ACheckProviderSomeWhere" />
   6: </object>

Posted in: Development  Tags: , , ,
Dan posted on June 11, 2009 09:26

I'm not sure if anyone else has spotted this but if you've found something useful and want to check the answer on Experts Exchange then you'll find the answers at the bottom of the page!  Just scroll past the bit about "subscribe to see this answer", keep scrolling past all the all the links and there they are!


Posted in: Development  Tags:

Here’s a quick timesaving keyboard shortcut that works in all Visual Studio versions to quickly comment and uncomment a line or selection of code.


 Quick Comment

Press Ctrl + K C.  You don’t need to select the whole line and VS will detect the type of code and use the correct type of comment.

 Quick Uncomment

Press Ctrl + K U.


Dan posted on June 4, 2009 10:59

I always forget how to do this:

Guid guid = new Guid("2b1ee12d-8056-4f44-bf5a-412d3cb8f948");

Posted in: Development  Tags:

Came across this nasty error the other day whilst trying to get an IIS app running on my Visat 64bit PC:

Error: Is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

Solution:

  1. Within IIS 7 click on Application Pools (left hand side under {machine_name})
  2. Select your app pool and then click Advanced Settings on the right.
  3. Second setting in the list: Enable 32-Bit Applications - must be set to True.

 


Posted in: Development  Tags: ,
Dan posted on June 2, 2009 23:44

If you use TortoiseSVN and just moved to TFS you'll notice that by default there's no shell integration! I personally like to use the shell to manage my checkins, so here's the solution:

  1. Install TFS Power Toys http://msdn.microsoft.com/en-us/teamsystem/bb980963.aspx
  2. Make sure you do a custom install and select "windows shell integration"


Thats it, just a reboot required!


Posted in: Development  Tags: , ,

Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Recent Comments

Banners

Theme Grabber
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Dan Gibbons .Net Developer