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


Sponsor

Recent Comments

  • Neil wrote: Fantastic demo. Cool!...
  • chandra wrote: Matt, Excellent Blog and am grateful to you for sharing your work. I'd like to suggest a small chan...
  • Ismael Solís wrote: thank you very much all the ideas that I've found here have been helpful. ...
  • Frank wrote: Hi I was wondering if you know why this is happening: I have a details page that contains a DetailsV...
  • benjib98 wrote: Hi Matt. I have found your resourceful blog and decided to extend your modal popup animator. Unfortu...
  • Gordon wrote: Thanks, that is absolute brilliant work....
  • jeremiah wrote: Matt, I've been pouring over your blog most of the day and was surprised to see you went to NDSU. Sh...
  • Justin Moses wrote: I get the same error as Sumith. It only happens when I use a drop down list to postback and update t...

Sponsor