Testing SqlDataType for null when exact type unknow until runtime

E

Earl Teigrob

I have a class that has only public fields of only SqlDataType types that
acts as a row(record) for my dataset table.

This "row" class is passed to one of my XML classes as a constructor and the
different methods in the XML class use its fields in the row class to
perform actions.

Several of my XML class methods use reflection to iterate through the fields
in the "row" class and determine whether each field value is null or not. I
have a way of performing this test but it seems very clumsy and I was
wondering of someone knew a more elegant(and efficient) way of accomplishing
the same task?

Thanks for your help!

Earl

Here is the code I am currently using...

//using reflection to iterate files of "row" class
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (SqlTypeIsNull(x))
v[RowIndex].Row[myFieldInfo.Name]=x;
}


//ugly test to determine if SqlType is null
private bool SqlTypeIsNull(object o)
{
string ValueType=o.GetType().ToString();
switch (ValueType)
{
case "System.Data.SqlTypes.SqlBinary":
if (((System.Data.SqlTypes.SqlBinary)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlBoolean":
if (((System.Data.SqlTypes.SqlBoolean)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlByte":
if (((System.Data.SqlTypes.SqlByte)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDateTime":
if (((System.Data.SqlTypes.SqlDateTime)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDecimal":
if (((System.Data.SqlTypes.SqlDecimal)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDouble":
if (((System.Data.SqlTypes.SqlDouble)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt16":
if (((System.Data.SqlTypes.SqlInt16)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt32":
if (((System.Data.SqlTypes.SqlInt32)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt64":
if (((System.Data.SqlTypes.SqlInt64)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlMoney":
if (((System.Data.SqlTypes.SqlMoney)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlSingle":
if (((System.Data.SqlTypes.SqlSingle)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlString":
if (((System.Data.SqlTypes.SqlString)o).IsNull)
return true;
break;
default:
return true;
}
return false;
}


//this is the class that represents my datatable row and is used to build
the dataset
public class XmlDataRecord_Test
{
public System.Data.SqlTypes.SqlInt32 MySqlInt1;
public System.Data.SqlTypes.SqlInt32 MySqlInt2;
public System.Data.SqlTypes.SqlDateTime MySqlDateTime1;
}
 
G

Greg Ewing [MVP]

Earl, would this work?

private bool SqlTypeIsNull(object o)
{
return (o == DBNull.Value);
}

--
Greg Ewing [MVP]
http://www.citidc.com




Earl Teigrob said:
I have a class that has only public fields of only SqlDataType types that
acts as a row(record) for my dataset table.

This "row" class is passed to one of my XML classes as a constructor and the
different methods in the XML class use its fields in the row class to
perform actions.

Several of my XML class methods use reflection to iterate through the fields
in the "row" class and determine whether each field value is null or not. I
have a way of performing this test but it seems very clumsy and I was
wondering of someone knew a more elegant(and efficient) way of accomplishing
the same task?

Thanks for your help!

Earl

Here is the code I am currently using...

//using reflection to iterate files of "row" class
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (SqlTypeIsNull(x))
v[RowIndex].Row[myFieldInfo.Name]=x;
}


//ugly test to determine if SqlType is null
private bool SqlTypeIsNull(object o)
{
string ValueType=o.GetType().ToString();
switch (ValueType)
{
case "System.Data.SqlTypes.SqlBinary":
if (((System.Data.SqlTypes.SqlBinary)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlBoolean":
if (((System.Data.SqlTypes.SqlBoolean)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlByte":
if (((System.Data.SqlTypes.SqlByte)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDateTime":
if (((System.Data.SqlTypes.SqlDateTime)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDecimal":
if (((System.Data.SqlTypes.SqlDecimal)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDouble":
if (((System.Data.SqlTypes.SqlDouble)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt16":
if (((System.Data.SqlTypes.SqlInt16)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt32":
if (((System.Data.SqlTypes.SqlInt32)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt64":
if (((System.Data.SqlTypes.SqlInt64)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlMoney":
if (((System.Data.SqlTypes.SqlMoney)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlSingle":
if (((System.Data.SqlTypes.SqlSingle)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlString":
if (((System.Data.SqlTypes.SqlString)o).IsNull)
return true;
break;
default:
return true;
}
return false;
}


//this is the class that represents my datatable row and is used to build
the dataset
public class XmlDataRecord_Test
{
public System.Data.SqlTypes.SqlInt32 MySqlInt1;
public System.Data.SqlTypes.SqlInt32 MySqlInt2;
public System.Data.SqlTypes.SqlDateTime MySqlDateTime1;
}
 
E

Earl Teigrob

Unfortunatly Not...

SqlTypes that are null can not be evaluated with DBNull.Value. They contain
their own instrinsic value for null that is assigned like

SqlInt32 i = SqlInt32.Null // is actually null by default

and tested by using

if (i.IsNull) ...

I wish there was a common 'SqlTypeNull' type that could be used against
all SqlTypes for a null condition, such at

if (i==SqlTypeNull)

I suppose creating overload '==' operators that test against a custom object
that represents "SqlTypeNull" would work...but first I must make sure there
is no other way...

Earl

Greg Ewing said:
Earl, would this work?

private bool SqlTypeIsNull(object o)
{
return (o == DBNull.Value);
}

--
Greg Ewing [MVP]
http://www.citidc.com




Earl Teigrob said:
I have a class that has only public fields of only SqlDataType types that
acts as a row(record) for my dataset table.

This "row" class is passed to one of my XML classes as a constructor and the
different methods in the XML class use its fields in the row class to
perform actions.

Several of my XML class methods use reflection to iterate through the fields
in the "row" class and determine whether each field value is null or
not.
I
have a way of performing this test but it seems very clumsy and I was
wondering of someone knew a more elegant(and efficient) way of accomplishing
the same task?

Thanks for your help!

Earl

Here is the code I am currently using...

//using reflection to iterate files of "row" class
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (SqlTypeIsNull(x))
v[RowIndex].Row[myFieldInfo.Name]=x;
}


//ugly test to determine if SqlType is null
private bool SqlTypeIsNull(object o)
{
string ValueType=o.GetType().ToString();
switch (ValueType)
{
case "System.Data.SqlTypes.SqlBinary":
if (((System.Data.SqlTypes.SqlBinary)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlBoolean":
if (((System.Data.SqlTypes.SqlBoolean)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlByte":
if (((System.Data.SqlTypes.SqlByte)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDateTime":
if (((System.Data.SqlTypes.SqlDateTime)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDecimal":
if (((System.Data.SqlTypes.SqlDecimal)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDouble":
if (((System.Data.SqlTypes.SqlDouble)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt16":
if (((System.Data.SqlTypes.SqlInt16)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt32":
if (((System.Data.SqlTypes.SqlInt32)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt64":
if (((System.Data.SqlTypes.SqlInt64)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlMoney":
if (((System.Data.SqlTypes.SqlMoney)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlSingle":
if (((System.Data.SqlTypes.SqlSingle)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlString":
if (((System.Data.SqlTypes.SqlString)o).IsNull)
return true;
break;
default:
return true;
}
return false;
}


//this is the class that represents my datatable row and is used to build
the dataset
public class XmlDataRecord_Test
{
public System.Data.SqlTypes.SqlInt32 MySqlInt1;
public System.Data.SqlTypes.SqlInt32 MySqlInt2;
public System.Data.SqlTypes.SqlDateTime MySqlDateTime1;
}
 
E

Earl Teigrob

I ended up just casting the sqldatatype back to the INullable interface and
used its IsNull property to test for null. Works like a charm.

Earl


Earl Teigrob said:
Unfortunatly Not...

SqlTypes that are null can not be evaluated with DBNull.Value. They contain
their own instrinsic value for null that is assigned like

SqlInt32 i = SqlInt32.Null // is actually null by default

and tested by using

if (i.IsNull) ...

I wish there was a common 'SqlTypeNull' type that could be used against
all SqlTypes for a null condition, such at

if (i==SqlTypeNull)

I suppose creating overload '==' operators that test against a custom object
that represents "SqlTypeNull" would work...but first I must make sure there
is no other way...

Earl

Greg Ewing said:
Earl, would this work?

private bool SqlTypeIsNull(object o)
{
return (o == DBNull.Value);
}

--
Greg Ewing [MVP]
http://www.citidc.com




Earl Teigrob said:
I have a class that has only public fields of only SqlDataType types that
acts as a row(record) for my dataset table.

This "row" class is passed to one of my XML classes as a constructor
and
the
different methods in the XML class use its fields in the row class to
perform actions.

Several of my XML class methods use reflection to iterate through the fields
in the "row" class and determine whether each field value is null or
not.
I
have a way of performing this test but it seems very clumsy and I was
wondering of someone knew a more elegant(and efficient) way of accomplishing
the same task?

Thanks for your help!

Earl

Here is the code I am currently using...

//using reflection to iterate files of "row" class
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (SqlTypeIsNull(x))
v[RowIndex].Row[myFieldInfo.Name]=x;
}


//ugly test to determine if SqlType is null
private bool SqlTypeIsNull(object o)
{
string ValueType=o.GetType().ToString();
switch (ValueType)
{
case "System.Data.SqlTypes.SqlBinary":
if (((System.Data.SqlTypes.SqlBinary)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlBoolean":
if (((System.Data.SqlTypes.SqlBoolean)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlByte":
if (((System.Data.SqlTypes.SqlByte)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDateTime":
if (((System.Data.SqlTypes.SqlDateTime)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDecimal":
if (((System.Data.SqlTypes.SqlDecimal)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDouble":
if (((System.Data.SqlTypes.SqlDouble)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt16":
if (((System.Data.SqlTypes.SqlInt16)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt32":
if (((System.Data.SqlTypes.SqlInt32)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt64":
if (((System.Data.SqlTypes.SqlInt64)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlMoney":
if (((System.Data.SqlTypes.SqlMoney)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlSingle":
if (((System.Data.SqlTypes.SqlSingle)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlString":
if (((System.Data.SqlTypes.SqlString)o).IsNull)
return true;
break;
default:
return true;
}
return false;
}


//this is the class that represents my datatable row and is used to build
the dataset
public class XmlDataRecord_Test
{
public System.Data.SqlTypes.SqlInt32 MySqlInt1;
public System.Data.SqlTypes.SqlInt32 MySqlInt2;
public System.Data.SqlTypes.SqlDateTime MySqlDateTime1;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top