Blogs

Paul Kimmel's Blog

Reading Guid Fields with the SafeRead Helper

     

Professional DevExpress ASP.NET Controls contains a sample that uses a helper method I created called SafeRead. SafeRead includes a check for DBNull and null and then converts the field to the correct (or desired) type. One reader asked if it could be modified to work with Guids. I created a simple table with a primary key and a Guid column and used the sample code (in Listing 1); it appears that SafeRead handles Guids fine.

Listing 1: A helper method that reads database fields with a built-in check for null.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SafeReadTest
{
  class Program
  {
    static void Main(string[] args)
    {
      string connectionString =
        @"Data Source=.\SQLEXPRESS;Initial Catalog=SafeReadTest;Integrated Security=True;Pooling=False";

      List<SafeReadTable> list = new List<SafeReadTable>();

      using (SqlConnection connection =
        new SqlConnection(connectionString))
      {
        connection.Open();
        string SQL = "SELECT * FROM SafeReadTable";
        SqlCommand command = new SqlCommand(SQL, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
          SafeReadTable o = new SafeReadTable();
          o.ID = SafeRead<int>("ID", reader, 0);
          o.GuidColumn = SafeRead<Guid>("GuidColumn", reader, Guid.NewGuid());
          list.Add(o);
        }
      }

      Array.ForEach<SafeReadTable>(list.ToArray(),
        o=>Console.WriteLine(o.GuidColumn));
      Console.ReadLine();

    }

    private static T SafeRead<T>(string fieldname,
      SqlDataReader reader, T defaultValue)
    {
      object o = reader[fieldname];
      return o == null || o == System.DBNull.Value ?
        defaultValue : (T)Convert.ChangeType(o, typeof(T));
    }

  }

  public class SafeReadTable
  {
    private int iD;
    public int ID
    {
      get { return iD; }
      set { iD = value; }
    }

    private Guid guidColumn;
    public Guid GuidColumn
    {
      get { return guidColumn; }
      set
      {
        guidColumn = value;
      }
    }
  }
}

A reasonable revision to custom entity classes is to use nullable types {e.g. int?}. This will let you assign null values instead of default values to entity properties.

Published Nov 29 2009, 07:53 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

 

Vincent said:

How about the performance?

November 29, 2009 7:02 PM
 

Gary L Cox Jr [DX-Squad] said:

Single letter variables are not good, especially for demo purposes.  

November 30, 2009 10:53 AM
 

Paul Kimmel (DevExpress) said:

Vincent:

I wouldn't worry about performance unless there were nested for loops using code like this or expensive web calls, stuff like that. The calls are going to be quick and very short.

November 30, 2009 12:45 PM
 

Paul Kimmel (DevExpress) said:

Gary:

Single letter variables are fine in short functions. Short functions are the key. I have never had any problems with short variables in short functions. Lots of short variables in big functions may be another story. But, of course, you are entitled to your views. I wonder what the genesis of that particular view is, in its entirety.

November 30, 2009 12:47 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.