Archive for the ‘.Net 2.0’ Category

Another “Why didn’t someone tell me this earlier” Post

90% of the projects I work on need to send emails, and most clients don’t allow developer SMTP servers floating around their network (for good reason). So a big headache ensues trying to get access to an Exchange or SMTP server somewhere. I have been doing this same dance for years. But today I find a blog post about sending emails without an SMTP server. The .Net calls to SMTP just write the emails out on the file system. It’s just configuration even. So much to know, so little time.

Posted on:
Posted in .Net 2.0, .Net 3.5, ASP.NET | Comments Off on Another “Why didn’t someone tell me this earlier” Post

Casting a Null

Found an interesting tidbit today. There is an ASP.NET page that is using session variables. The user has visited the page, and populated the session. But then the user lingers, and the session times out on a page that is not protected. The user then clicks a button, and the following line of C# code executes:

User foo = (User)Session[“bar”];

So what should happen here? An Exception? That’s what I would have thought. But not true. Since the session has expired, the session variable is now null, and you can cast a null into another data type having a value of null. Just as long as that type is nullable. So the line above is the same as:

User foo = (User)null;

which is perfectly valid.

I got the null reference exception down the line when trying to access a property of foo. So once again, defensive programming is the rule. Check your session variables for null!

Don’t Do This

In ASP.NET, the DataList control renders as a table. Opening a new table in the header and trying to get rows to render just won’t end well. Lots of strange browser behavior should be the tip off that this isn’t right.

eBooks on CodePlex

Wriju has a gread roundup of eBooks/guidance available on CodePlex.

Quick ADO.Net Tip

If you need to stop a datareader in the middle, instead of at the end, you have to call cancel on the command object. Calling the Close() method will result in a timeout exception.

using (SqlCommand cmd = new SqlCommand(sql, conn))
          XmlReader reader = cmd.ExecuteXmlReader();

while (!reader.EOF)
            if (this.Cancel)
            holder = reader.ReadOuterXml();


Posted on:
Posted in .Net 2.0 | Comments Off on Quick ADO.Net Tip

DataSets and Calculated Columns

Ran into a performance issue in a .Net remoting situation. A Winforms app is calling an application server asking for data. A relatively large DataSet (>10,000 rows, <6 columns) being passed over the wire was causing a performance problem. The database and application servers processed it quickly. Examining the transfer with WireShark showed that the transfer wasn’t so bad either. There was a flurry of data passed, and then a bunch of waiting on the client-side, with the client CPU usage around 50% the entire duration of the wait. Turns out there is a calculated column in one of the data tables. The column is not calculated on the application server-side, so as not to pass a bunch of data across the wire that would be unnecessary. The calc happens on the client. That was the source of the slowdown and CPU usage. In the end the solution to the problem was not using the calculated column, we found a different solution to fix the business problem. I suppose you could perform the calculation in the SQL statement that was ultimately filling the DataSet. That might take longer to transfer, but won’t slow down the client app.

ASP.NET Web Project Build Events

I have a web project (the original 2005 web project type, not a web application project) and had a problem getting files copied to the bin directory. Essentially, one of the library projects referenced by the web project has an XML file in the project output, but when the solution is built, the XML file in the bin directory of the library project is not pulled into to the bin directory of the web project. Of course, a post-build event seemed like the thing to do, but web projects don’t have support for that.

A little digging and I found this post by Scott Guthrie that describes a “Build Helper Project”. You simply add an empty class library project to your solution. You then use the build events in the empty project use to add build events to your web project. You just make sure the project build order is correct so the events get called when you need them.

Posted on:
Posted in .Net 2.0, ASP.NET | Comments Off on ASP.NET Web Project Build Events

Casting Text Data to Members in a Business Object via Reflection

I have a pipe-delimited text file that I am parsing and using to fill a collection of custom business objects. Instead of hard-coding the bits of each pipe-delimited line, I am using an XML file to map data items in the file to object members, like this XML fragment:


After parsing and matching the data element to the proper member name, I call this function:

public void SetDataMemberValue(string memberName, Member member, string data)
{    // get an instance      
     // of that object's type.
     Type objectType = member.GetType();
     PropertyInfo[] properties = objectType.GetProperties();
     foreach (PropertyInfo property in properties)
    {         if (string.Equals(property.Name, memberName, StringComparison.OrdinalIgnoreCase))
            object[] args = new object[1];
            args[0] = data;
            object result = null;
            if (property.PropertyType == data.GetType())
                //The type of the property matches the data's type
                result = data;
               result = property.PropertyType.InvokeMember("Parse", BindingFlags.InvokeMethod, null, result, args);
            if (result != null)
                property.SetValue(member, result, null);

The tricky bit is the InvokeMember call, which is casting my string to the appropriate type for the property. Of course this is they stylized version of the function, I removed the try-catch bits to make it smaller and readable for the acutal function. You should wrap the InvokeMember call with a try-catch as Parse methods can fail quite hard.

Posted on:
Posted in .Net 2.0 | Comments Off on Casting Text Data to Members in a Business Object via Reflection

A BCL Method to Set a String in Title Case in C#

No need to create a function yourself or reference a VB library: A great post by Rick Strahl:

public static string TitleCase(string input)


      return System.Threading.Thread.CurrentThread.



Posted on:
Posted in .Net 2.0 | Comments Off on A BCL Method to Set a String in Title Case in C#

Free Contract First Tool – WSCF

Thinktecture has released a Visual Studio 2005 compatible version of their free WSCF tool. WSCF is a Schema-Based Contract-First Web Services code generator. It allows you to design the messages, interfaces and data for contract-first style web services, acting as a replacement for XSD.exe and WSDL.exe. We have been using XSDObjectGen to do something similarly, but WSCF looks like it is much more flexible. For one thing, it generates collections using generics as List<T> instead of the non-type specific ArrayList that XSDObjectGen creates or the even more simplistic arrays that WSDL.exe creates. If you are using .Net 2.0, this is a great step forward for contract-first tools.

Posted on:
Posted in .Net 2.0 | Comments Off on Free Contract First Tool – WSCF