Wednesday, January 26, 2005

DataReader NULL values

 
One way to deal with possibly-NULL string values from a datareader is to check for NULL first, e.g.


string myvalue;
if (myDataReader["MyField"] != System.DBNull.Value)
myvalue = myDataReader.GetString(myDataReader.GetOrdinal("MyField"));
else
myvalue = "";


But a less-code way is to just let the Convert object handle them for you:

string myvalue = Convert.ToString(myDataReader["MyField"]);


It may not perform quite as well as explicit checking, though, so don't use it in a tight loop if you're concerned about performance.



Archives
September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005  

This page is powered by Blogger. Isn't yours?