April 25, 2007

Quick Tip: Use System.IO.Path.Combine instead of string concatenation

I came across a bug in our application the other day. The code at fault was very similar to the simple example below.  It assumed that the 'folderPath' argument the code in question was supplied with already had a trailing '\' character appended to the folder path.  Because this was not always the case, the code was failing.  The fix was simple – I replaced the string concatenation with a call to System.IO.Path.Combine. This method takes care of checking for this situation and handles it for you. The System.IO.Path class is loaded with other methods that are useful when doing common file system operations. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] folderPaths = new string[] { @"c:\folder\folders", @"c:\folder\folders\" }; string fileName = "fileName.txt"; foreach (string folderPath in folderPaths) { Console.WriteLine(Program.SmartFileStringBuilder(folderPath, fileName)); Console.WriteLine(Program.LessSmartFileStringBuilder(folderPath, fileName)); } // both print the samething: c:\folder\folders\fileName.txt, one...

ObjectWriter<T>

A recent project I worked on required moving CSLA business objects to and from a sql server database through stored procs (no adhoc queries were allowed). Overtime, and after a few refactoring sessions trying to normalize some of the CSLA property/field to stored proc parameter mapping code, a nice little reusable component emerged that allowed us to write code as follows: Download code /// <summary> /// /// </summary> public void Insert() { using (SqlConnection connection = new SqlConnection( ConfigurationManager.ConnectionStrings["simple_orm_unit_test"].ConnectionString)) { using (SqlCommand command = new SqlCommand()) { // init the command command.Connection = connection; command.CommandText = "dbo.uspCustomer_Insert"; command.CommandType = CommandType.StoredProcedure; // open the connection command.Connection.Open(); using (ObjectWriter<Customer> writer = new ObjectWriter<Customer>( new SqlServer.SqlServerFieldInfoMap<Customer>(), new SqlServer.SqlServerSprocDataParamBuilder<Customer>())) { writer.Insert(this, command); } } } } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem...

Topics: General .Net

ObjectReader<T>

A recent project I worked on required moving CSLA business objects to and from a sql server database through stored procs (no adhoc queries were allowed). Overtime, and after a few refactoring sessions trying to normalize some of the CSLA property/field to stored proc parameter mapping code, a nice little reusable component emerged that allowed us to write code as follows: Download code /// <summary> /// /// </summary> /// <returns></returns> public static Customer[] FindAll() { using (SqlConnection connection = new SqlConnection( ConfigurationManager.ConnectionStrings["simple_orm_unit_test"].ConnectionString)) { using (SqlCommand command = new SqlCommand()) { // init the command command.Connection = connection; command.CommandText = "dbo.uspCustomer_FindAll"; command.CommandType = CommandType.StoredProcedure; // open the connection command.Connection.Open(); using (ObjectReader<Customer> reader = new ObjectReader<Customer>( new SqlServer.SqlServerFieldInfoMap<Customer>(), command.ExecuteReader())) { return reader.ReadAll(); } } } } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; }...

Topics: General .Net

Export GridView to Excel

I thought this sounded like a pretty standard requirement so I figured there would be a nice snippet on msdn or in an msdn publication describing the official Microsoft solution.  I couldn’t find that, or any other approach that I was happy with.  I ran across a few sites that looked promising, but didn’t quite meet my requirements – I had to either override a page level method and do nothing (here) or else turn off event validation (here and here).  It also generally appeared that the people using these solutions were also running into a variety of issues with grids that allow sorting and paging - all of the grids I am working with allow both. The approach I settled on is very similar to the above links, but without the limitations of having to turn off event validation or override any page methods.  This gives me the...

Topics: ASP.NET , GridView

April 24, 2007

Simple .Net Reflection Utility Class

st1\:*{behavior:url(#ieooui) } I find that at least once in a project, I am digging into the System.Reflection msdn help page trying to identify what method calls and BindingFlags I need to make to lookup a field, property or method on an object and invoke it. The last time I did this; I took the code and moved it into a class. Another advantage that I found of using the Reflector class is that it makes my code a little more more readable.  It is immediately obvious that Reflector.StaticGetProperty(typeof(Cusomter), "FullName") .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html {...

Topics: General .Net


Consulting Services

Yep - I also offer consulting services. And heck, I'll do just about anything. If you enjoy my blog just drop me an email describing the work you need done.

Recent Comments

  • TimothyP wrote: I work with tons of developers who haven't even got a clue what Twitter of Facebook is. In fact, it'...
  • Janko wrote: Yes it's strange that people think it's ok to blog at work but consider unethical to use facebook. P...
  • Kam Lagan wrote: Hi Matt, The numbers do seem a little "off" to what you perceived the community would be like. But...
  • Siderite wrote: You're kidding me! So the people that don't blog think it is ok to blog at work (maybe because they ...
  • Guy Harwood wrote: Good to see the results. one typo.... 'What best describes your current job title?' appears twice,...
  • sirrocco wrote: One problem with : DEV I DEV II DEV III was that I didn't know what it meant :( .(is it common ...
  • Jack wrote: How many person fill the survery? Blog is very common, so I don't think 71.5% is high. Maybe our bos...
  • Andy wrote: Done. The developer1, developer2 question - not sure how meaningful the results are going to be for ...